有没有任何常见的解决方案如何使用不完整的语法?在我的情况下,我只想检测Delphi(Pascal)文件中的方法,这意味着procedures和functions.以下第一次尝试正在进行中
methods
: ( procedure | function | . )+
;
Run Code Online (Sandbox Code Playgroud)
但这是一个解决方案吗?还有更好的解决方案吗?是否可以通过动作停止解析(例如,在检测之后implementation).使用预处理器是否有意义?什么时候 - 如何?
任何人都可以推荐一个生成win32 Delphi代码的解析器生成器吗?我要做的是创建一个简单的领域特定语言.
我有以下输入
@Book{press,
author = "Press, W. and Teutolsky, S. and Vetterling, W. and Flannery B.",
title = "Numerical {R}ecipes in {C}: The {A}rt of {S}cientific {C}omputing",
year = 2007,
publisher = "Cambridge University Press"
}
Run Code Online (Sandbox Code Playgroud)
我必须为RecDescent解析器生成器编写语法.输出中的数据应该针对xml结构进行修改,并且应该如下所示:
<book>
<keyword>press</keyword>
<author>Press, W.+Teutolsky, S.+Vetterling, W.+Flannery B.</author>
<title>Numerical {R}ecipes in {C}: The {A}rt of {S}cientific {C}omputing</title>
<year>2007</year>
<publisher>Cambridge University Press</publisher>
</book>
Run Code Online (Sandbox Code Playgroud)
应将附加和重复字段报告为错误(带有行号的正确消息,不再进行解析).我试着从这样的事情开始:
use Parse::RecDescent;
open(my $in, "<", "parsing.txt") or die "Can't open parsing.txt: $!";
my $text;
while (<$in>) {
$text .= $_;
}
print …Run Code Online (Sandbox Code Playgroud) 我正在Jison中编写一个简单的表达式解析器.这是我的语法:
{
"operators": [
["left", "+", "-"],
["left", "*", "/", "%"]
],
"bnf": {
"program": [
["statement EOF", "return $1;"]
],
"statement": [
["expression NEWLINE", "$$ = $1 + ';';"]
],
"expression": [
["NUMBER", "$$ = yytext;"],
["expression binary expression", "$$ = $1 + $2 + $3;"]
],
"binary": [
["+", "$$ = ' + ';"],
["-", "$$ = ' - ';"],
["*", "$$ = ' * ';"],
["/", "$$ = ' / ';"],
["%", "$$ = ' % ';"], …Run Code Online (Sandbox Code Playgroud) 我正在使用Jison编写解析器.这是我的语法:
{
"program": [
["statements EOF", "return $1;"]
],
"statements": [
["statement", "$$ = $1;"],
["statements statement", "$$ = $1 + '\\n' + $2;"]
],
"statement": [
["expression NEWLINE", "$$ = $1 + ';';"]
],
"expression": [
["NUMBER", "$$ = yytext;"],
["expression expression", "$$ = $1 + ', ' + $2;"]
]
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我收到以下错误消息:
Conflict in grammar: multiple actions possible when lookahead token is NUMBER in
state 9
- reduce by rule: expression -> expression expression
- shift token (then …Run Code Online (Sandbox Code Playgroud) 我有以下文件,需要解析
--TestFile
Start ASDF123
Name "John"
Address "#6,US"
end ASDF123
Run Code Online (Sandbox Code Playgroud)
开头的--行将被视为注释行.并且文件以"开始"开始并以end.结束.后面的字符串Start是UserID然后,name并且address将在双引号内.
我需要解析文件并将解析后的数据写入xml文件.
所以生成的文件就像
<ASDF123>
<Name Value="John" />
<Address Value="#6,US" />
</ASDF123>
Run Code Online (Sandbox Code Playgroud)
现在我正在使用模式匹配(Regular Expressions)来解析上面的文件.这是我的示例代码.
/// <summary>
/// To Store the row data from the file
/// </summary>
List<String> MyList = new List<String>();
String strName = "";
String strAddress = "";
String strInfo = "";
Run Code Online (Sandbox Code Playgroud)
方法:ReadFile
/// <summary>
/// To read the file into a List
/// …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个解析器,它应该将Prolog列表(例如[1,2,3,4])解析到相应的Scala列表中.我使用Scalas解析组合器对解析器进行编程.
到目前为止,我的解析器看起来像这样:
class PListParser extends JavaTokenParsers{
def list:Parser[List[Any]] = "[" ~> listArgs <~ "]"
def listArgs:Parser[List[Any]] = list | repsep(args, ",")
def args:Parser[String] = "(.)*".r
}
Run Code Online (Sandbox Code Playgroud)
是否有可能将前两个解析器的类型参数转换为更具体的?类似于任意维度但具有相同底层类型的嵌套列表的一般参数.
给出以下词法分析器:
lexer grammar CodeTableLexer;
@header {
package ch.bsource.ice.parsers;
}
CodeTabHeader : OBracket Code ' ' Table ' ' Version CBracket;
CodeTable : Code ' '* Table;
EndCodeTable : 'end' ' '* Code ' '* Table;
Code : 'code';
Table : 'table';
Version : '1.0';
Row : 'row';
Tabdef : 'tabdef';
Override : 'override' | 'no_override';
Obsolete : 'obsolete';
Substitute : 'substitute';
Status : 'activ' | 'inactive';
Pkg : 'include_pkg' | 'exclude_pkg';
Ddic : 'include_ddic' | 'exclude_ddic';
Tab : 'tab';
Naming …Run Code Online (Sandbox Code Playgroud) 我正在使用PEG.js创建一个包含解析字符串的解析器。
包含任何类型字符的字符串都用引号括起来",并且可能包含转义引号\"。
到目前为止,我有以下规则:
start
= ["] string:(( '\\"' {return '"';} / [^"])*) ["]
{return string.join('');}
Run Code Online (Sandbox Code Playgroud)
它在PEG.js 在线版本中工作,并"abc\"def"针对给定的输入进行生成"abc\"def"。
为 Node.js 版本 0.6.21 和 PEG.js 版本 0.7.0 生成的解析器按以下方式执行
var result = parser.parse('"abc\"def"');
Run Code Online (Sandbox Code Playgroud)
并产生以下错误:
{ name: 'SyntaxError',
expected: [],
found: 'd',
message: 'Expected end of input but "d" found.',
offset: 5,
line: 1,
column: 6 }
Run Code Online (Sandbox Code Playgroud)
但是,使用\\"代替会\"成功并获得预期的输出。
var result = parser.parse('"abc\\"def"'); // parses correctly
Run Code Online (Sandbox Code Playgroud)
这个问题有解释或解决方法吗?特别是,我不可能对解析器的预期输入中的所有引号进行双重转义。
我对这里的所有编程爱好者都有一个问题.指针一直存在于编程世界中.就像在C,C++中得到明确的指针支持一样.在java中,显式指针支持不存在,但Java系统内部使用指针.在实际的世界中,是否可以开发一种编程语言来完全没有指针别名?