Question: Write a program in Prolog that parses a simple computer program written using a subset of the Clite syntax. Your program should take a computer
Write a program in Prolog that parses a simple computer program written using a subset of the Clite syntax.
Your program should take a computer program source file as input, and print a message indicating whether or not the program is syntactically correct (can just return true or false). The user must be able to run a query in the following form, where prog.txt is your program to test:
parse_program("prog.txt").
Therefore, you must have a rule called parse_program that takes one parameter, which is the name of your source program file.
The subset of the Clite syntax you must use is shown by the following EBNF grammar (meta-characters are in bold and blue). You will need to convert it to a BNF grammar and then you can use the DCG format (like the natural language programming example we discussed in class) to code the BNF grammar in Prolog.
program -> int main ( ) { declarations statements } declarations -> { declaration } declaration -> type identifier [ [digit] ] ; type -> int | bool | float | char statements -> { statement } statement -> ; | block | assignment | if_statement | while_statement block -> { statements } assignment -> identifier [ [digit] ] = expression ; if_statement -> if ( expression ) statement while_statement -> while ( expression ) statement expression -> conjunction { || conjunction } conjunction -> equality { && equality } equality -> relation [ equ_op relation ] equ_op -> == | != relation -> addition [ rel_op addition ] rel_op -> < | <= | > | >= addition -> term { add_op term } add_op -> + | - term -> factor { mul_op factor } mul_op -> * | / | % factor -> [ unary_op ] primary unary_op -> - | ! primary -> identifier [ [digit] ] | literal | ( expression ) | type ( expression ) literal --> digit | boolean identifier -> A | ... | Z boolean --> true | false digit --> 0 | ... | 9
Here is an example of converting one of the EBNF rules to the DCG format of Prolog:
relation --> addition. relation --> addition, rel_op, relation. rel_op --> ["<"]. rel_op --> ["<="]. and so on.
Terminals and non-terminals can also occur on the same line, separated by commas, like the following:
assignment --> id, ["="], expression, [";"].
You will need to split the string into a list of strings, so you may want to encode your terminals using double-quotes, like the examples above,
Also, expect the input program to have spaces around all lexemes, including around operators and punctuation, such as semi-colons. Below is an example program.
int main ( ) { int a ; float b ; a = a + b ; if ( a || b && c ) { c = d + e - ( a + ( b % c ) ) ; a = b ; } }
Your program must do the following:
Allow the user to submit a query to test whether a computer program is a syntactically valid program, as described above.
The query must accept an input file and output a message (can just be true or false) that indicates whether the program is syntactically correct, according to the Clite grammar above.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
