The goal of the project is to implement a syntax analyzer (a.k.a., parser) as we’ve learned. More specifically, you will implement the syntax analyzer for a simplified C programming language with the following context free grammar G within python3;
CFG G: 01: CODE→VDECL CODE | FDECL CODE | ϵ 02: VDECL→vtype id semi 03: FDECL→vtype id lparen ARG rparen lbrace BLOCK 𝐑𝐄𝐓𝐔𝐑𝐍 rbrace 04: ARG→vtype id MOREARGS | ϵ 05: MOREARGS→comma vtype id MOREARGS | ϵ 06: BLOCK→STMT BLOCK | ϵ 07: STMT→VDECL | id assign RHS semi 08: STMT→if lparen COND rparen lbrace BLOCK rbrace else lbrace BLOCK rbrace 09: STMT→while lparen COND rparen lbrace BLOCK rbrace | FCALL semi 10: RHS→EXPR | FCALL | literal 11: EXPR→TERM addsub EXPR | TERM 12: TERM→FACTOR multdiv TERM | FACTOR 13: FACTOR→lparen EXPR rparen | id | num 14: FCALL→id lparen ARG rparen 15: COND→FACTOR comp FACTOR 𝟏𝟔: 𝐑𝐄𝐓𝐔𝐑𝐍→𝐫𝐞𝐭𝐮𝐫𝐧 𝐅𝐀𝐂𝐓𝐎𝐑 𝐬𝐞𝐦𝐢
- vtype for the types of variables and functions
- num for signed integers
- literal for literal strings
- id for the identifiers of variables and functions
- if, else, while, and return for if, else, while, and return statements respectively
- addsub for + and - arithmetic operators
- multdiv for * and / arithmetic operators
- assign for assignment operators
- comp for comparison operators
- semi and comma for semicolons and commas respectively
- lparen, rparen, lbrace, and rbrace for (, ), {, and } respectively
CODE, VDECL, FDECL, ARG, MOREARGS, BLOCK, STMT, RHS, EXPR, TERM, FACTOR, COND, FCALL, RETURN
If you want to implement a bottom-up parser, then you are required 1) to construct an NFA for recognizing viable prefixes of G, 2) to convert the NFA into a DFA, 3) to compute follow sets, 4) to construct a SLR parsing table, and 5) to implement a SLR parser.