Flex和Bison的相关性难度(Flex and Bison Associativity difficulty)
使用Flex和Bison,我有一个布尔查询语言的语法规范,它支持逻辑“和”,“或”和“非”操作,以及使用“()”的嵌套子表达式。
一切都很好,直到我注意到像“A和B或C和D”这样的查询我想解析为“(A&B)|(C&D)”实际上被解释为“A&(B |( C&D))“。 我几乎可以肯定这是一个相关性问题,但似乎无法在任何地方找到正确的解释或例子 - 或者我错过了一些重要的事情。
来自boolpars.y的相关信息:
%token TOKEN %token OPEN_PAREN CLOSE_PAREN %right NOT %left AND %left OR %% query: expression { ... } ; expression: expression AND expression { ... } | expression OR expression { ... } | NOT expression { ... } | OPEN_PAREN expression CLOSE_PAREN { ... } | TOKEN { ... } ;
任何人都可以找到这个缺陷吗? 我不明白为什么Bison没有给出“或”适当的优先权。
Using Flex and Bison, I have a grammar specification for a boolean query language, which supports logical "and", "or", and "not" operations, as well as nested subexpressions using "()".
All was well until I noticed that queries like "A and B or C and D" which I'd like parsed as "(A & B) | (C & D)" was actually being interpreted as "A & ( B | ( C & D ) )". I'm nearly certain this is an associativity issue, but can't seem to find a proper explanation or example anywhere - that or I'm missing something important.
Pertinent information from boolpars.y:
%token TOKEN %token OPEN_PAREN CLOSE_PAREN %right NOT %left AND %left OR %% query: expression { ... } ; expression: expression AND expression { ... } | expression OR expression { ... } | NOT expression { ... } | OPEN_PAREN expression CLOSE_PAREN { ... } | TOKEN { ... } ;
Can anyone find the flaw? I can't see why Bison isn't giving "or" appropriate precedence.
原文:https://stackoverflow.com/questions/1046428
最满意答案
这是修改后的代码:
样本文件:
@这是一个咒语的描述。
我经常想知道我经常能经常写这个词。但是我觉得这个经常写出来。 这看起来不是很奇怪。 快速的棕色狐狸跳过小红篱笆。
@这是对咒语的另一种描述
苹果是水果。 它是红色的。
@ 3rd描述
我有一只宠物。 它是一只名叫约翰的狗。
@@@
public static ArrayList<String> descriptions = new ArrayList<String>(); public static void main(String[] args) throws IOException { File filename = new File("C:\\temp\\test.txt"); loadDescriptions(filename); System.out.println("*************"); for(String ln:descriptions){ System.out.println(ln); } } public static void loadDescriptions(File name) throws IOException { FileReader fr = new FileReader(name); BufferedReader br = new BufferedReader(fr); StringBuilder sb = new StringBuilder(); int i = 0; String line=null; while ((line = br.readLine()) != null ) { if(line.startsWith("@")){ if(i>0){ descriptions.add(sb.toString()); sb = new StringBuilder(); } }else{ if(!line.isEmpty()){ System.out.println(line); sb.append(" " + line); } } i++; } }
here is the modified code:
sample file:
@This is a description of a spell.
I often wonder how often I can write the word often.Without seeming that it is too often that I write this out. Does it not seem weird. The quick brown fox jumps over the small red fence.
@This is another description of a spell
Apple is fruit. It is red in colour.
@3rd description
I have a pet. Its a dog named john.
@@@
public static ArrayList<String> descriptions = new ArrayList<String>(); public static void main(String[] args) throws IOException { File filename = new File("C:\\temp\\test.txt"); loadDescriptions(filename); System.out.println("*************"); for(String ln:descriptions){ System.out.println(ln); } } public static void loadDescriptions(File name) throws IOException { FileReader fr = new FileReader(name); BufferedReader br = new BufferedReader(fr); StringBuilder sb = new StringBuilder(); int i = 0; String line=null; while ((line = br.readLine()) != null ) { if(line.startsWith("@")){ if(i>0){ descriptions.add(sb.toString()); sb = new StringBuilder(); } }else{ if(!line.isEmpty()){ System.out.println(line); sb.append(" " + line); } } i++; } }
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
沿着以下方向的东西: func arrayFromContentsOfFileWithName(fileName: String) -> [String]? { guard let path = NSBundle.mainBundle().pathForResource(fileName, ofType: "txt") else { return nil } do { let content = try String(contentsOfFile:p ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
搜索描述(Searching through descriptions)[2023-03-06]
你应该看看文件分类。 一些文档分类技术 朴素贝叶斯分类器 TF-IDF You should check out document classification. A few document classification techniques Naive Bayes classifier tf–idf -
自动加载一堆小类(Autoloading a bunch of small classes)[2023-04-28]
只有在使用Kernel#autoload方法访问特定模块时,才能加载特定文件。 autoload(:Foo, "foo.rb") autoload(:Bar, "bar.rb") ... You can let a particular file be loaded only when a particular module is accessed by using the Kernel#autoload method. autoload(:Foo, "foo.rb") autoload(:Bar, "b ... -
$parentarray = array(); foreach ($otherarray as $bar) { $parent = new stdClass; $parent->conversion = $bar; $parent->negative = $constant; $parentarray[] = $parent; // Or am I mistaken? } $parentarray = array(); foreach ($otherarray as $ba ...
-
这很简单。 打开三个文件进行写作的示例: with open('file1', 'w') as f1, open('file2', 'w') as f2, open('file3', 'w') as f3: # do stuff It's pretty simple. Example of opening three files for writing: with open('file1', 'w') as f1, open('file2', 'w') as f2, open('file3', ' ...
-
创建一个加载其他“标准”模块的模块是perl5i和Modern :: Perl之类的动机。 具有词法效果的Pragma模块,例如strict,warnings和autodie只需加载到模块的导入例程中。 需要通知导出函数的模块在其他地方导出模块,这可以通过Import :: Into完成 。 最后,类只需要加载。 由于use在编译时发生的,因此需要在运行时执行相应的操作,这require模块并调用其import方法。 这里是一个例子,打开严格和警告,加载Time :: Local和加载Time :: Pie ...
-
用C ++创建一堆对象(Creating a bunch of objects in C++)[2023-07-30]
你可以使用std :: vector来达到这个目的: http : //en.cppreference.com/w/cpp/container/vector #includestruct Address { std::string city; std::string street; std::string number; }; bool parseAddress(Address& address) { //TODO: implement //TODO: r ... -
如何将带有一堆对象描述的文件加载到数组中?(How do I load a file with a bunch of descriptions for objects into an array?)[2023-05-31]
这是修改后的代码: 样本文件: @这是一个咒语的描述。 我经常想知道我经常能经常写这个词。但是我觉得这个经常写出来。 这看起来不是很奇怪。 快速的棕色狐狸跳过小红篱笆。 @这是对咒语的另一种描述 苹果是水果。 它是红色的。 @ 3rd描述 我有一只宠物。 它是一只名叫约翰的狗。 @@@ public static ArrayListdescriptions = new ArrayList (); public static void main(String[] ar ...