问题
The following programming languages question seems really unclear and what it wants me to do is not obvious to me, could somebody help me to understand it?
The question:
Implement part of a recursive descent parser for a simple set of language rules. Use any programming language for the implementation; if it isn’t a common language, please make a note of which language it is in a comment. You should implement functions for each of the nonterminal symbols and, using the following rules that describe part of a C-like language.
<ifblock> --> if(<logic_expr>){<stmts>} [else {<stmts>}]
<logic_expr> --> <value> == <value> | <value> != <value>
You may assume the following functions:
lex(), a lexical analyser which places a code for the next token in the global variable next token
stmts()
value()
Terminal symbols should be encoded with the following defined constants:
CODE LP for ‘(’
CODE RP for ‘)’
CODE LB for ‘{’
CODE RB for ‘{’
CODE EQ for ‘==’
CODE NEQ for ‘!=’
CODE IF for ‘if’
CODE ELSE for ‘else’
回答1:
From what I can see: You are asked to write two functions
ifblock
logic_expr
as part of a recursive descent parser.
For the other non-terminal symbols, stmts
and value
you are allowed to assume the existence of pre-written functions by the same names.
How to write a recursive descent parser should have been discussed in the lectures and tutorials, so I assume you know how that works.
To get the next token from the input stream you can call lex()
which returns a code, as listed in the codes for the terminal symbols. What you need to do is to implement the ifblock
by requesting token codes by calling lex()
and to evaluate and match those with the required tokens according to the language grammar. Note that the else
part in the ifblock
is optional according to the grammar.
To evaluate the logical expression of the if
you need to step into a function logic_expr
, which you are asked to write as well, which evaluates a logical expression as defined in the grammar. Again, you may assume that the function for the non-terminal value
does already exist.
来源:https://stackoverflow.com/questions/60129100/can-somebody-walk-me-through-what-this-question-is-trying-to-ask-of-me