/*
* LR(1) 语法分析
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #include "Common.h"
#include "LRCal.h"
#include "LRMigrate.h"
#include "Stack.h" #define NEXTWORD(s) ((*(s)==0)?SYM_EOF:*((s)++)) extern char *Grammer[]; void LRParse(struct LRElement **LRTable, unsigned char *InputString)
{
printf("-------- Parse --------\n");
struct Stack *SymbolStack = BuildStack();
Push(SymbolStack, ); // push $;
Push(SymbolStack, ); // push start state s0;
int Word = NEXTWORD(InputString); // word <- NextWord();
int State = ;
while (true) // while (true)
{
State = Top(SymbolStack); // state <- top of stack;
printf("State: %d\tWord: %c \t", State, Word);
if (LRTable[State][Word].Action == Reduce) // if (Action[state, word] = "reduce A->β")
{
printf("reduce %d", (int)LRTable[State][Word].ActionValue); char *Production = Grammer[LRTable[State][Word].ActionValue - ];
int ProductSize = strlen(Production) - ;
// pop 2*|β| symbols;
ProductSize *= ;
while (ProductSize--)
{
Pop(SymbolStack);
}
// state <- top of stack;
State = Top(SymbolStack);
// push A;
int LeftUnterminal = (int)Production[];
Push(SymbolStack, LeftUnterminal);
// push Goto[State, A];
Push(SymbolStack, (int)LRTable[State][LeftUnterminal].ActionValue);
}
else if (LRTable[State][Word].Action == Shift) // else if (Action[state, word] = "shift si")
{
printf("shift %d", (int)LRTable[State][Word].ActionValue);
// push word;
Push(SymbolStack, Word);
// push si;
Push(SymbolStack, LRTable[State][Word].ActionValue);
// word <- NextWord();
Word = NEXTWORD(InputString);
}
else if (LRTable[State][Word].Action == Accept) // else if (Action[state, word] = "accept")
{
printf("accept\n");
break;
}
else
{
printf("Syntax Error!\n");
exit();
}
printf("\n");
}
printf("Grammer Parse Success!\n");
}

联动:LR(1)表生成算法演示程序

全部代码文件:http://files.cnblogs.com/rexfield/LR.zip

LR(1)表驱动语法分析程序的更多相关文章

  1. LR(1)表驱动语法分析设计图表

  2. Tomcat内核之ASCII解码的表驱动模式

    我们知道Tomcat通信是建立在Socket的基础上,而套接字在服务器端和客户端传递的报文都是未经过编码的字节流,每8位组成1个字节,计算机以二进制为基础,这是由于使用晶体管的开合状态表示1和0,这样 ...

  3. 表驱动方法(Table-Driven Methods)

    表驱动方法(Table-Driven Methods) - winner_0715 - 博客园 https://www.cnblogs.com/winner-0715/p/9382048.html W ...

  4. Table-Driven Design 表驱动设计

    注:本文所有代码来自 http://www.codeproject.com/Articles/42732/Table-driven-Approach 在许多程序中,经常需要处理那些拥有种种色色不同特性 ...

  5. Mysql优化原则_小表驱动大表IN和EXISTS的合理利用

    //假设一个for循环 ; $i < ; $i++) { ; $i < ; $j++) { } } ; $i < ; $i++) { ; $i < ; $j++) { } } ...

  6. 如何使用Microsoft的驱动程序验证程序解释无法分析的崩溃转储文件

    这篇文章解释了如何使用驱动程序验证工具来分析崩溃转储文件. 使用Microsoft驱动程序验证工具 如果您曾经使用Windows的调试工具来分析崩溃转储,那么毫无疑问,您已经使用WinDbg打开了一个 ...

  7. html表单验证程序

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. LR(1)表生成算法演示程序

    /* * LR 转换表 * + Goto 记录表 * + 状态转换表 */ #include <stdio.h> #include <stdlib.h> #include &l ...

  9. [lua]原来这才是表驱动的正确表达方式

    曾经写了个很煞笔的脚本模拟switch..case语法形式.[lua]尝试一种Case语法糖 而今实际项目应用中突发,原来这才是正确的表驱动方式表达.如下所贴: function event_do( ...

随机推荐

  1. html doctype 作用

    文档模式主要有以下两个作用: 1.告诉浏览器使用什么样的html或xhtml规范来解析html文档 2.对浏览器的渲染模式产生影响:不同的渲染模式会影响到浏览器对于 CSS 代码甚至 JavaScri ...

  2. mysql自动备份数据库

    可以选择设置需要备份的库,自动备份压缩,自动删除 7 天前的备份,需要使用 crontab 定时执行. #!/bin/bash # 要备份的数据库名,多个数据库用空格分开 databases=(db1 ...

  3. Sublime Text 3 LESS、SASS、SCSS高亮插件、提示插件

    为sublime text 添加LESS语法高亮 功能:LESS高亮插件   下载   https://packagecontrol.io/packages/LESS 简介:用LESS的同学都知道,s ...

  4. 安装mod_deflate模块启用apache的GZIP压缩

    安装mod_deflate模块启用apache的GZIP压缩 操作系统:Linux Cent OS 5 / Max OS X 10.6 snow leopard相关环境:Apache httpd 2. ...

  5. 服务器返回的JSON字符串

    异步请求将type设为"json",或者利 用$.getJSON()方法获得服务器返回,那么就不需要eval()方法,因为这时候得到的结果已经是json对象

  6. #Leet Code# Best Time to Buy and Sell Stock

    描述:数组 A,对于 i < j, 找到最大的 A[j] - A[i] 代码: class Solution: # @param prices, a list of integer # @ret ...

  7. linux下文件加密压缩和解压的方法

    一.用tar命令 对文件加密压缩和解压 压缩:tar -zcf  - filename |openssl des3 -salt -k password | dd of=filename.des3 此命 ...

  8. MINA源码阅读之Future系

    首先Future系是对某个异步操作完成的监听:即setValue()的完成情况监听:get/setValue其实是对result字段封装,由此,可以这样讲,Future系其实对于对result字段状态 ...

  9. ssh登录docker容器

    ssh登录docker容器1.启动一个docker容器# docker run -t -i ubuntu/ruby:v1 /bin/bash2.然后在容器里,安装openssh-server open ...

  10. PEP Index > PEP 339 -- Design of the CPython Compiler 译文

    http://www.python.org/dev/peps/pep-0339/ PEP: 339 标题: CPython的编译器设计 版本: 425fc5598ee8 最后修改: 2011-01-1 ...