I am trying to arrange for ocamllex and ocamlyacc code to scan and parse a simple language. I have defined the abstract syntax for the same but am finding difficulty scanning for complex rules. Here's my code
{
type exp = B of bool | Const of float | Iszero of exp | Diff of exp*exp |
If of exp * exp * exp
}
rule scanparse = parse
|"true"| "false" as boolean {B boolean}
|['0'-'9']+ "." ['0'-'9']* as num {Const num}
|"iszero" space+ ['a'-'z']+ {??}
|'-' space+ '(' space* ['a'-'z']+ space* ',' space* ['a'-'z']+ space* ')' {??}
But I am not able to access certain portions of the matched string. Since the expression declaration is recursive, nested functions aren't helping either(?). Please help.
To elaborate slightly on my comment above, it looks to me like you're trying to use ocamllex to do what ocamlyacc is for. I think you need to define very simple tokens in ocamllex (like booleans, numbers, and variable names), then use ocamlyacc to define how they go together to make things like Iszero, Diff, and If. ocamllex isn't powerful enough to parse the structures defined by your abstract syntax.
Update
Here is an ocamlyacc tutorial that I found linked from OCaml.org, which is a pretty good endorsement: OCamlYacc tutorial. I looked through it and it looks good. (When I started using ocamlyacc, I already knew yacc so I was able to get going pretty quickly.)