Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / Write a lexer and a parser using PLY (very much like that for the WAE minilanguage) for the simplified C language with the following grammar, with nonterminals surrounded by angular braces < >, the -> is a production, and everything else is literal (terminal) symbols: <stmt> -> <id> = <expr> ; <stmt> -> { <stmtlist> } <stmt> -> if ( <expr> ) <stmt> <stmt> -> while ( <expr> ) <stmt> <stmtlist> -> <stmt> <stmtlist> -> <stmtlist> <stmt> <expr> -> <id> <expr> -> <num> <expr> -> <expr> <optr> <expr> where <optr> can be any operator from the set {>=, <=, ==, >, <, +, -, *, /}, <id> can be any legal identifier in most programming languages, such as x, y, Xy, y_1, z2, etc

Write a lexer and a parser using PLY (very much like that for the WAE minilanguage) for the simplified C language with the following grammar, with nonterminals surrounded by angular braces < >, the -> is a production, and everything else is literal (terminal) symbols: <stmt> -> <id> = <expr> ; <stmt> -> { <stmtlist> } <stmt> -> if ( <expr> ) <stmt> <stmt> -> while ( <expr> ) <stmt> <stmtlist> -> <stmt> <stmtlist> -> <stmtlist> <stmt> <expr> -> <id> <expr> -> <num> <expr> -> <expr> <optr> <expr> where <optr> can be any operator from the set {>=, <=, ==, >, <, +, -, *, /}, <id> can be any legal identifier in most programming languages, such as x, y, Xy, y_1, z2, etc

Computer Science

Write a lexer and a parser using PLY (very much like that for the WAE
minilanguage) for the simplified C language with the following
grammar, with nonterminals surrounded by angular braces < >, the -> is
a production, and everything else is literal (terminal) symbols:

<stmt> -> <id> = <expr> ;
<stmt> -> { <stmtlist> }
<stmt> -> if ( <expr> ) <stmt>
<stmt> -> while ( <expr> ) <stmt>

<stmtlist> -> <stmt>
<stmtlist> -> <stmtlist> <stmt>

<expr> -> <id>
<expr> -> <num>
<expr> -> <expr> <optr> <expr>

where <optr> can be any operator from the set {>=, <=, ==, >, <, +, -,
*, /}, <id> can be any legal identifier in most programming languages,
such as x, y, Xy, y_1, z2, etc., and <num> can be any real number,
such as 8, -8, +9, 9., -9., +10.5, 3.14159, etc.

You need only write a lexer and a parser (like with the WAE example),
which outputs the "parse tree".  For example, since this language is
more general than that presented on slide 5, it can handle the input:

  if(x > 9) { x = 0; y = y + 1; }

outputting parse tree:

  ['if', [['id', 'x'], ['optr', '>'], ['num', 9.0]], [['id', 'x'], '=', ['num', 0.0], ['id', 'y'], '=', [['id', 'y'], ['optr', '+'], ['num', 1.0]]]]

but also something more general, like:

  while(x == 5.5) { x = x + 3.5; y = y * 2.5; }

outputting parse tree:

  ['while', [['id', 'x'], ['optr', '=='], ['num', 5.5]], [['id', 'x'], '=', [['id', 'x'], ['optr', '+'], ['num', 3.5]], ['id', 'y'], '=', [['id', 'y'], ['optr', '*'], ['num', 2.5]]]]

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Related Questions