grammar

How can error reporting in grammars be improved?

会有一股神秘感。 提交于 2019-11-26 14:36:15
问题 Is there a way to get Perl 6 to generate an error message if a grammar does not match? Or at least return the position of the last data it processed? It is quite hard to fix syntax errors if all I get from the parser is 'no match'. 回答1: If your focus is generating messages for users of your grammar, see Generating Good Parse Errors from a Parser and Grammar::ErrorReporting. The rest of this answer is about debugging. First, you can embed arbitrary closures (code) in Perl 6 rules (or tokens or

Left-Linear and Right-Linear Grammars

南楼画角 提交于 2019-11-26 14:14:35
I need help with constructing a left-linear and right-linear grammar for the languages below? a) (0+1)*00(0+1)* b) 0*(1(0+1))* c) (((01+10)*11)*00)* For a) I have the following: Left-linear S --> B00 | S11 B --> B0|B1|011 Right-linear S --> 00B | 11S B --> 0B|1B|0|1 Is this correct? I need help with b & c. Constructing an equivalent Regular Grammar from a Regular Expression First, I start with some simple rules to construct Regular Grammar(RG) from Regular Expression(RE). I am writing rules for Right Linear Grammar (leaving as an exercise to write similar rules for Left Linear Grammar) NOTE:

How to enumerate a recursive datatype in Haskell?

北慕城南 提交于 2019-11-26 14:08:54
问题 This blog post has an interesting explanation of how to use the Omega monad to enumerate an arbitrary grammar diagonally. He offers an example of how to do so, resulting in an infinite sequence of strings. I'd like to do the same, except that, instead of generating a list of strings, it generates a list of an actual datatype. For example, data T = A | B T | C T T Would generate A, B A, C A A, C (B A) A... Or something similar. Unfortunately my Haskell skills are still maturing and after some

What's a valid left-hand-side expression in JavaScript grammar?

一曲冷凌霜 提交于 2019-11-26 12:12:50
问题 Okay, we all know what the valid left-hand-side expressions are. Kind of.* But, looking at the definition from the ECMA-Script standard, I\'m very confused: LeftHandSideExpression : NewExpression CallExpression Is that just an error in the definition, or am I getting something wrong here? I mean, doesn\'t that actually mean that new Object = 1; // NewExpression AssignmentOperator PrimaryExpression function () { return foo; }() = 1;// CallExpression AssignmentOperator PrimaryExpression are

What is the language of this deterministic finite automata?

戏子无情 提交于 2019-11-26 11:25:32
问题 Given: I have no idea what the accepted language is. From looking at it you can get several end results: 1.) bb 2.) ab(a,b) 3.) bbab(a, b) 4.) bbaaa 回答1: How to write regular expression for a DFA In any automata, the purpose of state is like memory element. A state stores some information in automate like ON-OFF fan switch. A Deterministic-Finite-Automata(DFA) called finite automata because finite amount of memory present in the form of states. For any Regular Language(RL) a DFA is always

English grammar for parsing in NLTK

旧巷老猫 提交于 2019-11-26 10:21:51
问题 Is there a ready-to-use English grammar that I can just load it and use in NLTK? I\'ve searched around examples of parsing with NLTK, but it seems like that I have to manually specify grammar before parsing a sentence. Thanks a lot! 回答1: You can take a look at pyStatParser, a simple python statistical parser that returns NLTK parse Trees. It comes with public treebanks and it generates the grammar model only the first time you instantiate a Parser object (in about 8 seconds). It uses a CKY

int a[] = {1,2,}; Weird comma allowed. Any particular reason?

痞子三分冷 提交于 2019-11-26 06:25:47
Maybe I am not from this planet, but it would seem to me that the following should be a syntax error: int a[] = {1,2,}; //extra comma in the end But it's not. I was surprised when this code compiled on Visual Studio, but I have learnt not to trust MSVC compiler as far as C++ rules are concerned, so I checked the standard and it is allowed by the standard as well. You can see 8.5.1 for the grammar rules if you don't believe me. Why is this allowed? This may be a stupid useless question but I want you to understand why I am asking. If it were a sub-case of a general grammar rule, I would

Left-Linear and Right-Linear Grammars

天涯浪子 提交于 2019-11-26 03:49:27
问题 I need help with constructing a left-linear and right-linear grammar for the languages below? a) (0+1)*00(0+1)* b) 0*(1(0+1))* c) (((01+10)*11)*00)* For a) I have the following: Left-linear S --> B00 | S11 B --> B0|B1|011 Right-linear S --> 00B | 11S B --> 0B|1B|0|1 Is this correct? I need help with b & c. 回答1: Constructing an equivalent Regular Grammar from a Regular Expression First, I start with some simple rules to construct Regular Grammar(RG) from Regular Expression(RE). I am writing

int a[] = {1,2,}; Weird comma allowed. Any particular reason?

你离开我真会死。 提交于 2019-11-26 01:57:09
问题 Maybe I am not from this planet, but it would seem to me that the following should be a syntax error: int a[] = {1,2,}; //extra comma in the end But it\'s not. I was surprised when this code compiled on Visual Studio, but I have learnt not to trust MSVC compiler as far as C++ rules are concerned, so I checked the standard and it is allowed by the standard as well. You can see 8.5.1 for the grammar rules if you don\'t believe me. Why is this allowed? This may be a stupid useless question but I

Is C++ context-free or context-sensitive?

大憨熊 提交于 2019-11-26 01:45:58
问题 I often hear claims that C++ is a context-sensitive language. Take the following example: a b(c); Is this a variable definition or a function declaration? That depends on the meaning of the symbol c . If c is a variable , then a b(c); defines a variable named b of type a . It is directly initialized with c . But if c is a type , then a b(c); declares a function named b that takes a c and returns an a . If you look up the definition of context-free languages, it will basically tell you that