Giter Club home page Giter Club logo

xyz-exp-ast's Introduction

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)

xyz-exp-ast's People

Contributors

jmid avatar

Stargazers

Tan, Kian-ting avatar Michael Vaquier avatar C For C's Sake avatar Yoshitaka Sakurai avatar Jahfer Husain avatar  avatar

Watchers

James Cloos avatar  avatar C For C's Sake avatar  avatar

Forkers

cfgeraldes

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.