https://github.com/jmid/xyz-exp-ast
A simple example of using ocamllex and menhir to build an abstract syntax tree
https://github.com/jmid/xyz-exp-ast
Last synced: 11 months ago
JSON representation
A simple example of using ocamllex and menhir to build an abstract syntax tree
- Host: GitHub
- URL: https://github.com/jmid/xyz-exp-ast
- Owner: jmid
- License: unlicense
- Created: 2014-07-01T12:10:55.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2014-08-14T08:30:14.000Z (almost 12 years ago)
- Last Synced: 2024-03-15T19:14:27.044Z (over 2 years ago)
- Language: OCaml
- Size: 137 KB
- Stars: 5
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
XYZ expressions
===============
This is a simple example of how ocamllex and menhir can be used to
build and process an abstract syntax tree (AST) for a language.
It was developed as part of (a previous version of) the undergraduate
compiler course at Aarhus University.
The AST is defined in ast.ml. It is built recursively in the action
statements of each grammar production in parser.mly, and it is
traversed in both pprint.ml and eval.ml.
Supported syntax:
exp ::= x | y | z
| exp + exp
| exp - exp
| exp * exp
| exp / exp
| ( exp )
To build:
---------
$ make
To run:
-------
Example 1:
$ ./xyz 3 4 5 (pass a command line value for variable x,y, and z, respectively)
x + y - z (then press return and Ctrl-D on Linux or MacOSX)
((x+y)-z)
2$
(hence the string 'x + y - z' is accepted, it is parsed as ((x+y)-z),
and it evaluates to 2 when x=3, y=4, and z=5)
Example 2:
$ ./xyz 3 4 5
x + y--
Parser.Error
$
(hence the string 'x + y--' is rejected by the parser)
Example 3:
$ ./xyz 3 4 5
2 + 2
Failure in lexing: empty token
$
(hence the string '2 + 2' is rejected by the scanner)