简体   繁体   English

LL1布尔运算符Grammar实现Recursive的解析器

[英]LL1 boolean operator Grammar to implement Recursive decent parser

Here is the small chunk from original Grammar for which i have to implement the recursive decent parser. 这是来自原始语法的小块,我必须实现递归的正确解析器。 We have to remove the ambiguity, left recursion etc from it so that we can implement its parser. 我们必须从中删除歧义,左递归等,以便我们可以实现其解析器。 I have done the other bits, but can't figure out how to handle the not operator (~). 我已完成其他位,但无法弄清楚如何处理not运算符(〜)。

The valid expression could be. 有效的表达式可能是。 1 & ~1, (1 & ~1) etc 1&〜1,(1&~1)等

I have handle the braces, as well as & and or symbol but can't handle the ~ symbol. 我有处理大括号,以及&和或符号但不能处理〜符号。

Here is the orginal grammar. 这是原始语法。

A -> A & A 
A -> ~A
A -> (A)
A -> 0 | 1

I can't figure out how to handle the ~ . 我无法弄清楚如何处理

Here is my solution: 这是我的解决方案:

one -> two one'
one' -> ~one|^
two -> three two'
two' -> & three two'|^
three -> four three'
three' -> || four three' | ^
four -> (one) |0 |1 

When i implemented it , everything works fine for brackets, and , or operator . 当我实现它时,一切都适用于括号,或者操作符。 But the negation ~ was not working. 但否定〜没有奏效。 So i believe the grammer is not properly converted to LL(1) 所以我认为语法没有正确转换为LL(1)

Finally, i have solved it myself after spending quite a lot of time. 最后,我花了很多时间自己解决了这个问题。 Here is the solution. 这是解决方案。

The boolean not or (~) operator has the highest precedence then any other operator in the grammar mentioned above. 布尔值not或(〜)运算符的优先级高于上述语法中的任何其他运算符。 In the above solution, i was taking its precedence as lower. 在上面的解决方案中,我的优先级更低。 Here is the proper solution. 这是正确的解决方案。

图像显示解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM