在浏览了寻找好的C#解析器生成器的帖子后,我偶然发现了GPLEX和GPPG。我想使用GPLEX生成令牌,以便GPPG解析和创建树(类似于lex/yacc关系)。然而,我似乎找不到一个关于这两者如何相互作用的例子。使用lex/yacc,lex返回由yacc定义的标记,并且可以将值存储在yylval中。在GPLEX/GPPG中是如何做到这一点的(他们的文档中没有)?
附件是我想要转换成GPLEX的lex代码:
%{
#include <stdio.h>
#include "y.tab.h"
%}
%%
[Oo][Rr] return OR;
[Aa][Nn][Dd] return AND;
[Nn][Oo][Tt] return NOT;
[A-Za-z][A-Za-z0-9_]* yylval=yytext; return ID;
%%谢谢!安德鲁
发布于 2015-09-21 19:38:38
首先:在你的项目中包含引用"QUT.ShiftReduceParser.dll“。它在GPLEX的下载包中提供。
Main-Program的示例代码:
using System;
using ....;
using QUT.Gppg;
using Scanner;
using Parser;
namespace NCParser
{
class Program
{
static void Main(string[] args)
{
string pathTXT = @"C:\temp\testFile.txt";
FileStream file = new FileStream(pathTXT, FileMode.Open);
Scanner scanner = new Scanner();
scanner.SetSource(file, 0);
Parser parser = new Parser(scanner);
}
}
} 示例-GPLEX代码:
%using Parser; //include the namespace of the generated Parser-class
%Namespace Scanner //names the Namespace of the generated Scanner-class
%visibility public //visibility of the types "Tokens","ScanBase","Scanner"
%scannertype Scanner //names the Scannerclass to "Scanner"
%scanbasetype ScanBase //names the Scanbaseclass to "ScanBase"
%tokentype Tokens //names the Tokenenumeration to "Tokens"
%option codePage:65001 out:Scanner.cs /*see the documentation of GPLEX for further Options you can use */
%{ //user-specified code will be copied in the Output-file
%}
OR [Oo][Rr]
AND [Aa][Nn][Dd]
Identifier [A-Za-z][A-Za-z0-9_]*
%% //Rules Section
%{ //user-code that will be executed before getting the next token
%}
{OR} {return (int)Tokens.kwAND;}
{AND} {return (int)Tokens.kwAND;}
{Identifier} {yylval = yytext; return (int)Tokens.ID;}
%% //User-code Section示例-GPPG input-file的代码:
%using Scanner //include the Namespace of the scanner-class
%output=Parser.cs //names the output-file
%namespace Parser //names the namespace of the Parser-class
%parsertype Parser //names the Parserclass to "Parser"
%scanbasetype ScanBase //names the ScanBaseclass to "ScanBase"
%tokentype Tokens //names the Tokensenumeration to "Tokens"
%token kwAND "AND", kwOR "OR" //the received Tokens from GPLEX
%token ID
%% //Grammar Rules Section
program : /* nothing */
| Statements
;
Statements : EXPR "AND" EXPR
| EXPR "OR" EXPR
;
EXPR : ID
;
%% User-code Section
// Don't forget to declare the Parser-Constructor
public Parser(Scanner scnr) : base(scnr) { }c#parsegppggplex
发布于 2013-11-01 07:54:37
我遇到了一个类似的问题--由于明显缺乏文档,我不知道如何在GPPG中使用我的GPLEX输出。我认为问题源于这样一个事实,即GPLEX发行版包含gppg.exe和gplex.exe,但只包含GPLEX的文档。
如果你去GPPG主页下载这个发行版,你会得到GPPG的文档,其中描述了对输入文件的要求,如何构造你的语法等等。哦,你还会得到两个二进制文件- gppg.exe和gplex.exe。
在一个包中包含所有内容看起来会更简单。这绝对可以消除一些困惑,特别是对于那些可能对词法分析(标记化)和解析(可能还不是100%熟悉两者之间的差异)的人来说。
所以不管怎样,对于那些第一次这样做的人:
GPLEX http://gplex.codeplex.com -用于标记化/扫描/词法分析(同样的事情)
GPPG http://gppg.codeplex.com/ -将标记器的输出作为输入进行解析。例如,解析器使用语法,可以做简单的记号赋予器不能做的事情,比如检测括号集是否匹配。
发布于 2019-06-12 01:11:16
不久前,我同样需要同时使用GPLEX和GPPG,为了让这项工作变得更容易,我在Visual Studio中创建了一个nuget包,用于同时使用GPPG和GPLEX。
这个包可以安装在基于.Net框架的C#项目中,并向Visual Studio中的包管理器控制台添加一些命令。这个命令可以帮助你配置C#项目,以便在构建过程中集成GPPG和GPLEX。基本上,在您的项目中,您将编辑YACC和LEX文件作为源代码,并且在项目的构建过程中,将生成解析器和扫描器。此外,命令-let将定制解析器和扫描器所需的文件添加到项目中。
你可以在这里找到它:https://www.nuget.org/packages/YaccLexTools/
这里有一个博客文章的链接,解释了如何使用它:http://ecianciotta-en.abriom.com/2013/08/yacclex-tools-v02.html
https://stackoverflow.com/questions/10808564
复制相似问题