Reference no: EM131384944
Programming Assignment:
Implement a lexical and syntax analyzer based on the following grammar. Your analyzer should read an input test program from a file and then determine if it contains a syntax error. It does not have to show where the syntax error occurs or what kind of error it is.
<program> → begin <statement_list> end
<statement_list> → <statement> {;<statement_list>}
<statement> → <assignment_statement> | <loop_statement>
<assignment_statement> → <variable> = <expression>
<variable> → identifier (An identifier is a string that begins with a letter followed by 0 or more letters and/or digits)
<expression> → <variable> { (+|-) <variable>}
<loop_statement> → loop (<logic_expression>) <statement>
<logic_expression> → <variable> (< | >) <variable> (Assume that logic expressions have only less than or greater than operators)
Use the examples below to test your analyzer.
Input program with no syntax errors:
begin
total = var1 + var2;
loop (var1 < var2)
loop (var3 > var4)
var 2 = var2 - var 1
end
Input programs containing syntax errors:
total = var1 + var2;
loop (var1 < var2)
loop (var3 > var4)
var 2 = var2 - var 1
end
The keyword begin is missing
begin
total = var1 + var2;
loop (var1 < var2)
loop (var3 > var4)
var 2 = var2 - var 1
end
The last statement shouldn't end with a semicolon.
begin
total = var1 + var2;
loop (var1 < var2)
var 2 = var2 - var1
end
The keywords loops is misspelled.