ll1语法分析器c语言E->E T,算术表达式的 LL(1)语法分析器

算术表达式的 LL(1)语法分析器

张会霞

(辽宁师范大学 计算机与信息技术学院,辽宁 大连 116000)

摘要:语法分析是编译程序的核心部分,对其进行研究有着重要意义。本文介绍了编译过程语法分析阶段使用的LL(1)预测分析法的相关概念,并以算术表达式为例,对LL(1)语法分析器的构成及具体实现进行了详细的阐述。

关键词:语法分析;算术表达式;LL(1)文法;LL(1)语法分析器;

ArithmeticExpressions of LL(1) Parser

Zhang Huixia

(College of Computer and Information Technology, Liaoning Normal University, Dalian Liaoning 116000, China)

Abstract:Syntax analysis is the core part of compiler.It is of great significance to study it .This paper introduces the relevant concepts of LL(1) predictive analysis method used in the parsing stage of compilation process,and takes arithmetic expressions for example to elaborate the structure and concrete implementation of LL(1) parser.

Key words:syntax analysis; arithmetic expressions; LL(1) grammar; LL(1) parser

前言

编译程序极大地推动了计算机

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单LL(1)语法分析器的C++代码,用于解析给定文法的输入字符串: ```cpp #include <iostream> #include <stack> #include <map> #include <vector> #include <string> using namespace std; // 定义文法规则 vector<string> grammar = { "E->TE'", "E'->+TE'|ε", "T->FT'", "T'->*FT'|ε", "F->a|(E)" }; // 定义预测分析表 map<pair<string, string>, string> parse_table = { {{"E", "a"}, "TE'"}, {{"E", "("}, "TE'"}, {{"E'", "+"}, "+TE'"}, {{"E'", ")"}, "ε"}, {{"E'", "$"}, "ε"}, {{"T", "a"}, "FT'"}, {{"T", "("}, "FT'"}, {{"T'", "+"}, "ε"}, {{"T'", "*"}, "*FT'"}, {{"T'", ")"}, "ε"}, {{"T'", "$"}, "ε"}, {{"F", "a"}, "a"}, {{"F", "("}, "(E)"} }; // 定义符号栈和输入串栈 stack<string> symbol_stack; stack<char> input_stack; // 初始化输入串栈 void init_input_stack(string input) { for (int i = input.length() - 1; i >= 0; i--) { input_stack.push(input[i]); } input_stack.push('$'); } // 获取产生式右部 string get_production(string left_symbol, string input_symbol) { if (parse_table.find({left_symbol, input_symbol}) != parse_table.end()) { return parse_table[{left_symbol, input_symbol}]; } return ""; } // LL(1)语法分析 bool parse(string input) { // 初始化符号栈和输入串栈 symbol_stack.push("$"); symbol_stack.push("E"); init_input_stack(input); // 语法分析 while (!symbol_stack.empty()) { string top_symbol = symbol_stack.top(); char top_input = input_stack.top(); if (top_symbol == "$" && top_input == '$') { return true; } if (top_symbol == top_input || top_symbol == "ε") { symbol_stack.pop(); input_stack.pop(); } else if (top_symbol == "a" && top_input == 'a') { symbol_stack.pop(); input_stack.pop(); } else if (top_symbol == "(" && top_input == ')') { symbol_stack.pop(); input_stack.pop(); } else if (top_symbol == "E" || top_symbol == "T" || top_symbol == "F") { string production = get_production(top_symbol, string(1, top_input)); if (production == "") { return false; } symbol_stack.pop(); if (production != "ε") { for (int i = production.length() - 1; i >= 0; i--) { symbol_stack.push(string(1, production[i])); } } } else { return false; } } return true; } int main() { string input = "a*(a+a)$"; if (parse(input)) { cout << "输入串 " << input << " 符合文法规则" << endl; } else { cout << "输入串 " << input << " 不符合文法规则" << endl; } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值