I have got one question about writing recursive descent parsing for checking pascal grammar. I have got this code for example:
a := c ;
I see that a,c is variables. := and ; - is terminals. This expression I can check. But if I have开发者_如何学运维 got smth like this:
a := c + 1 - d ;
I have got problems how to write recursive descent parsing for this expression.
For first example I wrote on C# like this:
if ((!parsing(current_token, "var")) || (!current_token, ":=")) || ( !parsing(current_token, "var") && !parsing(current_token, "const") ) || (!current_token, "term"))) show_error();
How can I write for second example? Thanks.
Variable assignment in Pascal has the following BNF
variable := expression
So you need to parse the expression following the :=
token. As you tokenize the code you need to determine what is valid as the next token, so in this case you see that you have a variable followed by a := operator so you should now recurse into your expression parser function.
Writing a good expression parser is critical for parsing any language of value, you will find expressions everywhere e.g. For loops, If statements, Case statements etc.
I did a quick BING and found this BNF for Pascal which might help
http://www2.informatik.uni-halle.de/lehre/pascal/sprache/pas_bnf.html
精彩评论