【编译原理】语法分析实验
Install dependencies:
pipenv install
Enter Python virtual environment:
pipenv shell
Input files are located at:
.
└── test
├── input.c // Input C file
├── input.token.xml // Tokenized C file (from last lexcial analysis)
└── parse.xml // Created XML file (from this syntatic analysis)
Main executables are located at:
.
├── grammar.txt // Grammar file
├── main.py // Main python file
├── parserGeneral.py // Create parse tree
└── parserUtils.py // Analyze grammar, create LL(1) analyze table
Run a test with command:
python -u main.py
Which will produce the following outputs:
- Grammar:
- Terminal and non-terminal symbols:
- First and follow sets:
- LL(1) Parse table:
- Analyze stack:
- Grammar tree:
- XML Document:
firstsetgrammar.txt
is for testing purposed only. You can try this grammar by uncommenting the following lines inmain.py
:
# 5. Demo: 分析一个串 i+i*i
inputString = 'i+i*i'
parserGeneral.parseInputString(
inputString, grammar, terminalSymbols, nonTerminalSymbols, analyzeTable)
# Then comment the following lines in main.py until the end of the main function.
logLevel = 10
will produce verbose logs, for most circumstances,logLevel = 1
will be sufficient.