Comments (9)
On Sat, Aug 18, 2012 at 9:51 PM, callumenator [email protected]:
When i try to create a grammar module with semantic actions, like
asModule("parser","
Number < [0-9]+ {doStuff} "
);The generated grammar always gives:
static assert(false,
Bad grammar: ["PEGGED.Grammar failure at pos [index: 25, line: 1, col: 24]", "Pegged.EOI failure at pos [index: 25, line: 1, col: 24]"]
);Are semantic actions currently implemented? What is the correct way to use
them?Yes yes, they are implemented (I use them in the xml example).
Hmm.
OK, I think I get it: as written, the PEG grammar allows for only one
suffix. I guess I never used it otherwise.
I just pushed a modification in Pegged allowing for more than one suffix.
It seems to generate the correct code, but I haven't tested it heavily,
obviously. Can you pull this new version of Pegged and tell me if it does
what you need?
from pegged.
For the example I gave, that latest pull does fix it, but that example was just a trivial one I made up for testing (sorry!). The grammar I am actually trying to add a semantic action to is the arithmetic expression example you give. Without any semantic actions it works fine, however if i do this:
Expression < Expr
Expr < {doStuff} Factor AddExpr*
AddExpr < ('+'/'-') Factor
Factor < Primary MulExpr*
MulExpr < ('*'/'/') Primary
Primary < Parens / Number / Variable / '-' Primary
Parens < '(' Expr ')'
Number < [0-9]+
Variable < Identifier
I get that same bad grammar assertion. It doesn't seem to matter where I put the {doStuff}
. Maybe I am trying to use it the wrong way.
from pegged.
Actually that latest pull now breaks the arithmetic example completely for me. Just the grammar straight from arithmetic.d gives this aasertion when built as module:
static assert(false, `Bad grammar: ["PEGGED.Grammar failure at pos [index: 24, line: 1, col: 23]", "PEGGED.Definition failure at pos [index: 24, line: 1, col: 23]", "PEGGED.Expression failure at pos [index: 24, line: 1, col: 23]", "PEGGED.Sequence failure at pos [index: 24, line: 1, col: 23]", "PEGGED.Suffix failure at pos [index: 24, line: 1, col: 23]", "Pegged.Or(OPTION, ONEORMORE, ZEROORMORE, NamedExpr, WithAction) failure for sub-expr # 0 `OPTION` at pos [index: 24, line: 1, col: 23]", "PEGGED.OPTION failure at pos [index: 24, line: 1, col: 23]", "Pegged.Lit!(?) failure at pos [index: 24, line: 1, col: 23]"]`);
from pegged.
On Sun, Aug 19, 2012 at 1:32 AM, callumenator [email protected]:
For the example I gave, that latest pull does fix it, but that example was
just a trivial one I made up for testing (sorry!). The grammar I am
actually trying to add a semantic action to is the arithmetic expression
example you give. Without any semantic actions it works fine, however if i
do this:Expression < Expr
Expr < {doStuff} Factor AddExpr*
AddExpr < ('+'/'-') Factor
Factor < Primary MulExpr*
MulExpr < ('*'/'/') Primary
Primary < Parens / Number / Variable / '-' Primary
Parens < '(' Expr ')'
Number < [0-9]+
Variable < IdentifierI get that same bad grammar assertion. It doesn't seem to matter where I
put the {doStuff}. Maybe I am trying to use it the wrong way.
A semantic action must be put after an expression, like
Factor {doStuff} AddExpr* {otherStuff}
If you want an entire rule to be affected by an action, either put it in
parentheses:
(Factor AddExpr*) {doStuff}
or use the 'action-arrow', without any intervening space between the '<'
and the action:
Expr <{doStuff} Factor AddExpr*
As for the pull, I realized afterwards that I should have added the
possibility for more than one prefix also :(
from pegged.
Actually that latest pull now breaks the arithmetic example completely for
me. Just the grammar straight from arithmetic.d gives this aasertion when
built as module:static assert(false,
Bad grammar: ["PEGGED.Grammar failure at pos [index: 24, line: 1, col: 23]", "PEGGED.Definition failure at pos [index: 24, line: 1, col: 23]", "PEGGED.Expression failure at pos [index: 24, line: 1, col: 23]", "PEGGED.Sequence failure at pos [index: 24, line: 1, col: 23]", "PEGGED.Suffix failure at pos [index: 24, line: 1, col: 23]", "Pegged.Or(OPTION, ONEORMORE, ZEROORMORE, NamedExpr, WithAction) failure for sub-expr # 0
OPTIONat pos [index: 24, line: 1, col: 23]", "PEGGED.OPTION failure at pos [index: 24, line: 1, col: 23]", "Pegged.Lit!(?) failure at pos [index: 24, line: 1, col: 23]"]
);Indeed...
That was a '+' instead of a '*' . The parser was waiting for at least one
suffix after each expression.
I corrected that and also allowed for more and one prefix. I just pushed
it, sorry for the bug.
from pegged.
Yep it was those Factor {doStuff} AddExpr* {otherStuff}
type expressions that were failing, that last update seems to fix it all, many thanks!
Another small issue (which can be worked around with parenthesis around the RHS expressions like you mentioned above):
Rule1 <{Action1}
works, while Rule1 <-{Action1}
and Rule1 <~{Action1}
do not.
from pegged.
On Sun, Aug 19, 2012 at 10:44 PM, callumenator [email protected]:
Yep it was those Factor {doStuff} AddExpr* {otherStuff} type expressions
that were failing, that last update seems to fix it all, many thanks!nk
Good to know, and thanks for reporting.
I'm working on a new engine behind Pegged (still top-down recursive-descent
for now), and I find it to be one-to-two orders of magnitude faster. (50
times faster occurs frequently). I'm slowly upgrading the engine to get all
the current functionalities working, so stay tuned.
Another small issue (which can be worked around with parenthesis around
the RHS expressions like you mentioned above):
Rule1 <{Action1} works, while Rule1 <-{Action1} and Rule1 <~{Action1} do
not.
That's because I defined <{ ...}
to be an arrow by itself. I guess I
could delete this arrow and just use the convention that a lone action
block by itself before any sub-rule means the action should be applied to
the entire rule, but this changes the associativity of action blocks: most
of the times, they are after the rules they affect.
Oh, btw, while I think about it: another contributor commit templatized
ParseTree
, Output
and other parts of Pegged. So for a semantic action,
you should not use:
Output myAction(Output o)
{
...
}
The docs need to be updated on this (TODO). You should use a function
template:
O myAction(O)(O o)
{
...
}
That way your action can be applied on the current templated structs.
from pegged.
I'm working on a new engine behind Pegged (still top-down recursive-descent
for now), and I find it to be one-to-two orders of magnitude faster.
That's awesome news! I have only been using pegged the last couple of days (writing a little VM for an OpenGL interpreter) and it is just sooo cool to go from string grammar to AST with a simple Rule.parse(), and have semantic actions executed as they are parsed. I'll definitely stay tuned.
from pegged.
On Sun, Aug 19, 2012 at 11:13 PM, callumenator [email protected]:
I'm working on a new engine behind Pegged (still top-down
recursive-descent
for now), and I find it to be one-to-two orders of magnitude faster.That's awesome news! I have only been using pegged the last couple of days
(writing a little VM for an OpenGL interpreter) and it is just sooo cool to
go from string grammar to AST with a simple Rule.parse(), and have semantic
actions executed as they are parsed. I'll definitely stay tunedThat's great you like Pegged! The engine is almost OK (just have to deal
with parameterized rules and grammars). For now, it parses strings only (no
wstring or dstring), since I found it to represent 99% of my need, even for
my non-English language.
from pegged.
Related Issues (20)
- Grammar won't parse whole text, it stops short a few lines... HOT 1
- Comment syntax HOT 7
- Unwanted space consumption in rule parameter
- Syntax wrappers HOT 1
- How does one do Intellisense using a Pegged grammar? HOT 3
- Is there a way to break up a grammar into D classes, each with their own data to parse into & thus subgrammar? HOT 2
- I can't find the Wikipedia ParseTree handling code in the Pegged wiki any more... HOT 1
- Will there be a speed-up when using a Dlang switch-case and shorter pegged variable names? HOT 2
- Can we create a grammar induction algorithm based upon expected Pegged parsing failures? HOT 2
- How do you use dub + pegged + asModule? HOT 1
- Bump new version HOT 7
- Remove obsolete CI.
- How do you branch on complex names such as caseSensitiveLiteral!("let") (they get much more complicated) HOT 1
- Undocumented features of Pegged HOT 2
- Remove Leading and trailing automatic space capture HOT 6
- core.exception.ArraySliceError@pegged\pegged\peg.d(1930): slice [0 .. 18446744073709551612] extends past source array of length 0 HOT 7
- v0.4.9: Invalid grammar errors are not reported HOT 29
- GrammarTester enhancment proposal HOT 5
- Recreating D's identifier-delimited string literals HOT 4
- Status of dgrammar.d? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pegged.