Compilation principle - SLR (1) syntax analyzer (C/C++ code implementation)


0 Experiment purpose

Design, compile, implement and debug SLR(1) parser to deepen understanding of parser.

1 Experimental Requirements

According to the arithmetic expression grammar learned in the compilation theory course and the LR analysis table of the grammar, use C language to write a grammar analyzer that accepts arithmetic expressions as input, and complete it with the console (or text file, or combined with the lexical analyzer) ) as input, the console (or file) outputs the analysis results in the form of production sequences.

2 Experimental content

Realize LR syntax analyzer, example of execution process: analyze id+id*id, input id+id*id# according to the predictive analysis table on PPT, and analyze the popped and output content.
Grammar:
E'->E
E->E+T
E->T
T->T*F
T->F
F->(E)
F->id

3 Experiment ideas

1. First, I defined 7 functions, namely: the most important SLRScanner() function (for SLR grammar analysis); statestack() function (output state stack); signstack() function (output symbol stack); print( ) function (remaining string output); Action() function (for analysis and output action description); xfind() function (for finding predictive analysis variables); yfind() function (for finding predictive analysis table columns variable). Next, a two-dimensional array of strings is defined to store the predictive analysis table, and a two-dimensional character array is defined to store the extended grammar; two stacks, symbol stack and state stack, are defined.
2. The most important thing in this experiment program is the SLRScanner() function. Next, a brief analysis of this is carried out: at the beginning, the flag variable flag It is equal to 0 (do the initial shift operation), and continuously update the row variable and column variable to facilitate the next table lookup, and then push 0, # into the state stack and symbol stack respectively. Then use the xfind and yfind functions to search and assign values ​​to the row variables and column variables, and judge the data in the corresponding predictive analysis table: if it is equal to 0, it does not conform to the grammar, return. If not, the corresponding symbols and states are pushed into the symbol stack and the state stack respectively, and at the same time, the remaining character strings and corresponding action descriptions are output. If equal to 's', set flag to 0, otherwise equal to 1. If flag is equal to 1, perform the specification (compare SR(1) with the corresponding extended grammar and simultaneously operate on the symbol stack, state stack, remaining strings and action description, and output the corresponding production formula.) If flag is equal to 2, do the shift operation after goto, and then compare it with the predictive analysis table. If SR is equal to 0, it proves that the result of the table lookup is wrong; if SR is equal to acc, the program ends, and the output string conforms to the grammar.
3. Other functions are also more important. They are called in the SLR grammar analysis function. Through the operation of the stack, the output of symbols and states is realized, which greatly simplifies the program; the Action function helps output action descriptions; and xfind and yfind help find predictive analysis tables, both play an indispensable role.

4 Experiment code


5 Experimental results


6 Experiment summary

这个实验要求不尽相同,需要改一下,可以与我私信讨论。

7 实验程序以及实验报告下载链接

编译原理实验:包括实验一词法分析器,实验二进制分析,实验三语法分析器,实验四SLR语法分析器等其中含有实验报告,实验代码等等-C++文档类资源-CSDN文库

Guess you like

Origin blog.csdn.net/qq_58773908/article/details/128567906