Disclaimer: I’m new to the language.
I’m trying to figure out how to implement a sort of symbol-table in menhir. I would like to know whether it’s possible at all (I searched a bit in the manual but couldn’t find anything past parameters which I don’t see useful for this) or maybe it’s not the right design choice and there’s a standard way to handle it.
For illustration purposes, consider the following lexer+parser for a language to set identifiers to integer values and print them:
(* lexer.mll *)
{
open Parser
let get = Lexing.lexeme
}
rule token = parse
| "print" { PRINT }
| "set" { SET }
| ['a'-'z']+ { ID (get lexbuf) }
| ['0'-'9']+ { INT (int_of_string (get lexbuf)) }
| eof { EOF }
(* parser.mly *)
%token PRINT SET EOF
%token <int> INT
%token <string> ID
%start <unit> prog
%%
prog:
| l=list(stmt) EOF { }
stmt:
| SET id=ID v=INT { (* TODO: update symbol table with <id,v> *) }
| PRINT id=ID { (* TODO: check symbol table for value of id } }
How to pass the symbol table information into the actions? Or is the right way to generate a parse tree without considering environment and operate on that afterwards?
Thanks.