$30
Objective:
To learn the lexical and syntax analysis of context-free grammars using lex&yacc and to design a syntax analyzer for your own pogramming language.
Background Information:
Read “lexyacc.pdf” in the course homepage.
Implementation:
You will design a lexical and syntax analyzer for your own programming language. Give some rules for your programming language and call it as MPL (My Programming Language). For example, your rules may be as;
- all programs must start with “begin” and end with “end”
- all commands must be ended by “;”
- all variables must be declared just after “begin” in the following format; int: i num;
float: fl;
- three types exist; “int”, “float”, “char”
- variables names may be any alphanumeric string, but they must start with a letter
- statements between “begin” and “end” can be either a variable declaration or an assignment - an assignment includes four type of operators; +,-,*,/.
- the number of variables or constants in an expression is unlimited - the presedence of operators given as.............. - ........
- .etc
The minimum requirements are: a well defined regular grammar for variable names, rules for variable declerations, at least 4 arithmetic, 2 logical and 2 comparison operators with their presedence and associativity rules, at least one conditional statement (like if, switch) and at least one loop structure (like for, while, do-while, repeat-until...). Instead of using the grammar for C langugae, try to bring new ideas for your language.
After writing CFG rules for your language, design and implement a syntax analyzer for it. This analyzer will include a lex source (e.g mpl.l), a yacc source (e.g mpl.y) and example inputs (source codes written in your language, e.g myprog.mpl).
To test your analyzer, follow the steps below;
$ lex mpl.l
$ yacc mpl.y
$ gcc –o mpl y.tab.c –lfl
$ mpl < myprog.mpl
In the last step, if there is no syntax error in myprog.mpl, the program should give an “OK” message, otherwise, a “syntax error” message. Check your program for correct and wrong inputs.
Note: Do not deal with any semantic rule, just give syntax rules!