Reference no: EM132268870
Programming Languages Project - Parsing simple C programs
Assignment: F# program to parse simple C programs
This is part 1 of a two-part programming project in F#. Here in part 1, we're going to parse simple C programs using recursive descent, and produce three results:
1. Is the input program a valid simple C program? True or false
2. If the program is not valid, a syntax error message of the form "expecting X, but found Y".
3. If the program is valid, an equivalent simple ASM program.
Note that "simple C" is really very simple C --- no pointers, no arrays, no types other than integer, no loops, very simple expressions, etc. Here's an example simple C program ----------->
Regarding bullet #3 above... If the input program is valid, then the result is an equivalent simple ASM program. Given the simple C program shown to the right, the equivalent simple ASM program is shown below. Notice that simple ASM is a list of lists, where each sub-list denotes one instruction: $ASSIGN, $DECL, $EMPTY, $IF, $INPUT, and $OUTPUT.
BNF definition of simple C
What follows is the BNF definition of the simple C syntax. Notice that the first rule ends with $ --- the EOF token. In other words, a valid simple C program ends with } followed immediately by EOF.
Recursive descent parsing
In class on Monday we introduced the concept of recursive descent parsing (PDF, PPT). The idea is to write a function for each rule in the BNF, where each rule parses that aspect of the language. In the case of simple C, there will be functions for <stmt>, <empty>, <vardecl>, <input>, <output>, etc. When writing a given function, the approach is to match each token and call the function for each rule.
Your job is to write the compiler.parser.parse function. BTW, the name of the simple C input file is passed as a command-line argument.
Requirements -
No imperative programming. In particular, this means no mutable variables, no loops, and no data structures other than F# lists and tuples. Use recursion and higher-order approaches, though you must adhere to the recursive descent approach, which implies recursion is the dominant strategy. Do not change "main.fs" nor "lexer.fs", you must work with those components as given. Modify and extend only the parser component in "parser.fs".
Attachment:- Assignment File.rar