/*
* 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. [转] HTML5终极备忘大全(图片版+文字版)---张鑫旭

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=1544 一.前言兼图片 ...

  2. 小笔记(三):PHP使用thinkphp3.2.3对数组进行分页

    之前写过thinkphp3.2.3直接在查询数据的时候进行分页,前段时间用到了将查询之后的数组进行整理后进行分页,用到的一个函数array_slice($arr, $start, $length,tr ...

  3. onbeforeunload、beforeunload

    <script type="text/javascript"> function addOnBeforeUnload(e) {     var ev = e || ev ...

  4. html5 API

    1.Canvas绘图 2.postMessage跨域.多窗口传输 3.requestAnimationFrame动画 4.PageVisibility API页面可见性 5.File 本地文件操作 6 ...

  5. 在ECSHOP首页今日特价(促销商品)增加倒计时效果

    看到不少朋友在找首页特价商品倒计时的修改方法,写了这篇文章希望能帮到有此需要的朋友们 1.首先修改程序部分 打开includes/lib_goods.php 找到get_promote_goods() ...

  6. 自己实现的库函数2(memset,memcmp,memcpy,memmove)

    memset,memcmp,memcpy,memmove是对内存进行管理的库函数,为了更好的理解和使用这几个函数,自己用C语言实现一下~ //内存设置函数void* my_memset(void* d ...

  7. GPUImage 滤镜头文件

    #import "GLProgram.h" // Base classes #import "GPUImageOpenGLESContext.h" #impor ...

  8. C#中判断bool 类型 代码的最短写法

    看到一个关于写最短代码的,  是一个bool类型判断的:    public bool IsNull(object val) { if (val == null) { return true; } e ...

  9. UIPageControll - 图片格式

    设置pageCon的显示风格: 1. 颜色 page.pageIndicatorTintColor = [UIColor redColor]; page.currentPageIndicatorTin ...

  10. Aircrack-ng官方文档翻译[中英对照]---Airmon-ng

    Aircrack-ng官方文档翻译---Airmon-ng Description[简介] This script can be used to enable monitor mode on wire ...