题
我建立名为C--的假想的编程语言(不是实际的C--语言)的分析器。我已经得到了我需要翻译的语言的语法到的东西Pyparsing可以接受的阶段。不幸的是,当我来到我的解析输入字符串(这是正确的,应该不会造成Pyparsing错误),它不能正确解析。我担心这是由于我的语法错误,但我开始Pyparsing首次,我似乎无法看到我要去哪里错了。
我已经上传我是从翻译这里人有一个语法通读。
修改:强>更新与来自保罗的建议
这是目前我已经得到了语法(语法定义的两个顶部线是非常坏我的,我知道):
# Lexical structure definition
ifS = Keyword('if')
elseS = Keyword('else')
whileS = Keyword('while')
returnS = Keyword('return')
intVar = Keyword('int')
voidKeyword = Keyword('void')
sumdiff = Literal('+') | Literal('-')
prodquot = Literal('*') | Literal('/')
relation = Literal('<=') | Literal('<') | Literal('==') | \
Literal('!=') | Literal('>') | Literal('=>')
lbrace = Literal('{')
rbrace = Literal('}')
lparn = Literal('(')
rparn = Literal(')')
semi = Literal(';')
comma = Literal(',')
number = Word(nums)
identifier = Word(alphas, alphanums)
# Syntax definition
term = ''
statement = ''
variable = intVar + identifier + semi
locals = ZeroOrMore(variable)
expr = term | OneOrMore(Group(sumdiff + term))
args = ZeroOrMore(OneOrMore(Group(expr + comma)) | expr)
funccall = Group(identifier + lparn + args + rparn)
factor = Group(lparn + expr + rparn) | identifier | funccall | number
term = factor | OneOrMore(prodquot + factor)
cond = Group(lparn + expr + relation + expr + rparn)
returnState = Group(returnS + semi) | Combine(returnS + expr + semi)
assignment = Group(identifier + '=' + expr + semi)
proccall = Group(identifier + lparn + args + rparn + semi)
block = Group(lbrace + locals + statement + rbrace)
iteration = Group(whileS + cond + block)
selection = Group(ifS + cond + block) | Group(ifS + cond + block + elseS + block)
statement = OneOrMore(proccall | assignment | selection | iteration | returnState)
param = Group(intVar + identifier)
paramlist = OneOrMore(Combine(param + comma)) | param
params = paramlist | voidKeyword
procedure = Group(voidKeyword + identifier + lparn + params + rparn + block)
function = Group(intVar + identifier + lparn + params + rparn + block)
declaration = variable | function | procedure
program = OneOrMore(declaration)
我想知道是否有我的对面,我可以做什么样的改进,使之简化,而坚持我一直在考虑语法,翻译语法犯任何错误
修改2:强>更新以包括新的错误
下面是我解析输入字符串:
int larger ( int first , int second ) {
if ( first > second ) {
return first ;
} else {
return second ;
}
}
void main ( void ) {
int count ;
int sum ;
int max ;
int x ;
x = input ( ) ;
max = x ;
sum = 0 ;
count = 0 ;
while ( x != 0 ) {
count = count + 1 ;
sum = sum + x ;
max = larger ( max , x ) ;
x = input ( ) ;
}
output ( count ) ;
output ( sum ) ;
output ( max ) ;
}
这是错误消息从终端运行我的程序时,我得到:
/Users/Joe/Documents/Eclipse Projects/Parser/src/pyparsing.py:1156: SyntaxWarning: null string passed to Literal; use Empty() instead
other = Literal( other )
/Users/Joe/Documents/Eclipse Projects/Parser/src/pyparsing.py:1258: SyntaxWarning: null string passed to Literal; use Empty() instead
other = Literal( other )
Expected ")" (at char 30), (line:6, col:26)
None
解决方案
1)改变到Literal("if")
Keyword("if")
(等,下降到Literal("void")
),以防止相匹配的前导 “如果” 的命名"ifactor"
变量。
2)nums
,alphas
和alphanums
不是表达式,它们都是字符串,可以与字类使用定义,如“一个数是由NUMS的一个字“单词”的时候,以限定字符的一些典型的集”或‘的标识符是与α开头的单词,其后是零个或更多alphanums’。因此,而不是:
number = nums
identifier = alphas + OneOrMore(alphanums)
您想
number = Word(nums)
identifier = Word(alphas, alphanums)
3)相反Combine
的,我想你想Group
。当你想匹配的令牌是连续的,没有中间的空格使用Combine
,并会连接这些令牌,并返回它们作为一个字符串。 Combine
通常在情况下使用这样的:
realnum = Combine(Word(nums) + "." + Word(nums))
无Combine
,解析"3.14"
将返回字符串['3', '.', '14']
的名单,所以我们增加Combine
因此,对于realnum解析结果是'3.14'
(然后你可以传递给解析动作转换为实际的浮动值3.14
)。 Combine
s中间没有空格的实施还让我们意外地解析'The answer is 3. 10 is too much.'
和思考"3. 10"
代表实数。
4)这不应该引起你的错误,但多余的空格您输入字符串具有的大量的。如果你得到你的语法的工作,你应该能够解析"int x;"
一样好"int x ;"
。
希望有这些提示的让你去。你读过任何在线pyparsing文章或教程?并请通过在线的例子看。你需要得到Word
,Literal
,Combine
等如何执行各项任务的解析把握好。
5)您有误实施了长期和语句的递归定义。代替将它们分配''
的,写:
term = Forward()
statement = Forward()
然后,当你去确实与其递归定义定义它们,使用<<
操作(并确保封闭RHS在()
的)。
term << (... term definition ...)
statement << (... statement definition ...)
可以在这里找到一个递归解析器的一个例子一>,和基本pyparsing使用这里 - 请参见标题为“分析词典”的详细步骤的分步上的递归处理方式。