1

Here's the sample code of an OCaml parser:

%{ open Ast %}

%token PLUS MINUS TIMES DIVIDE EOF
%token <int> LITERAL

%left PLUS MINUS
%left TIMES DIVIDE

%start expr
%type < Ast.expr> expr

%%

expr:
  expr PLUS   expr { Binop($1, Add, $3) }
| expr MINUS  expr { Binop($1, Sub, $3) }
| expr TIMES  expr { Binop($1, Mul, $3) }
| expr DIVIDE expr { Binop($1, Div, $3) }
| LITERAL          { Lit($1) }

I have the code for the scanner and AST (Abstract Syntax Tree) too. What does $1 and $3 here indicate?

ADDENDUM: I want to assign a value to a variable. I also want to store the values of all the variables in an array. How can I do that?

Thomash
  • 6,339
  • 1
  • 30
  • 50
P.C.
  • 651
  • 13
  • 30
  • Variable assignment in OCaml is done using `ref` type: see http://ocaml.org/learn/tutorials/pointers.html – didierc Jul 19 '14 at 07:49
  • Array cell assignment is done using the left arrow syntax: https://realworldocaml.org/v1/en/html/imperative-programming-1.html – didierc Jul 19 '14 at 07:55
  • "I want to assign a value to a variable" you probably want to be more explicit about which variable you are talking about in that sentence. – didierc Jul 19 '14 at 07:58

2 Answers2

1

They refer to the values returned by the parse of the first and third parts of the grammar rule right hand side. In other words, to the values of the subexpressions. In the last case, $1 refers to the value of the literal.

Update

There are two languages involved here, so it's hard to know what you're asking. I would guess that you want your language to include the ability to have variables and to assign values to them. For that, your scanner has to have a token for variable names and a token for assignment. You need to expand your grammar to include assignment (perhaps as an operator with a value, as in C).

I would also guess that you want to keep an OCaml array with values of variables. You can do this with a global variable (impure but effective). Or you can pass an array up through the parse tree with the rest of your parse state.

A hash table or map might be better for representing variable names along with their values.

I wonder if your language is going to allow a given variable to be given values at more than one place.

Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108
  • Thanks. :) I added in something to the question above. Can you help me with that as well? – P.C. Jul 19 '14 at 03:59
0

This is actually the syntax of ocamlyacc which you should not use if you start a new project, in that case you should use menhir which has a more readable syntax (and is better for numerous other reasons). Some people want to rewrite the OCaml compiler using menhir but it seems that nobody has done it yet.

The menhir syntax is:

expr:
  e1 = expr PLUS   e2 = expr { Binop(e1, Add, e2) }
| e1 = expr MINUS  e2 = expr { Binop(e1, Sub, e2) }
| e1 = expr TIMES  e2 = expr { Binop(e1, Mul, e2) }
| e1 = expr DIVIDE e2 = expr { Binop(e1, Div, e2) }
| l = LITERAL                { Lit(l) }
Thomash
  • 6,339
  • 1
  • 30
  • 50