You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
mainly the crash is due to left-recursive rules: Expression < Expression Operator Expression, for example.
Parsing Expression Grammars do not handle left-recursive rules, as
explained in the docs. You should rewrite your rules for Expression and
Tinary, so as not to have subrules beginning with Expression, else the
parser enters an infinite loop (which causes the segmentation fault).
As a first approach, using # to comment the left-recursive subrules in
Expression (including Tinary) makes the example work.
There are two other small mistakes:
your definition of Identifier only recognizes two-letter
identifiers: *Identifier < ~([a-z_A-Z]) ~([a-z_A-Z0-9])*, you should use: Identifier < [a-z_A-Z][a-z_A-Z0-9]* to recognize any C-style identifier.
your Operator definition has no ''? You should use Operator < "+" / "-"
/ "" / "'"
Also, be aware that defining Script as Statement* allows the parsing of 0
characters as a valid parse. Hence not well-written modules will be parsed
as "" (the 0-Statement option). You should maybe define Script as Statement* endOfInput to catch any mistake.
Bastiaan Veelo recently add left-recursion capability to Pegged.
If you're still interested in it, you could try your grammar anew with the current HEAD.
i wrote the following grammar and generate it with asModule
Metacode:
Script < Statement*
Operator < "+" / "-" / "" / ""
Unary < "++" / "--" / "+" / "-"
UnaryPostFix < "++" /"--"
Identifier < ~([a-z_A-Z]) ~([a-z_A-Z0-9])
Variable < Identifier / "(" Expression ")"
Tinary < Expression "?" Expression ":" Expression
ArrayInitializer < "[" (Expression ",")* "]"
Expression < Unary Variable
/ Variable UnaryPostFix
/ Variable
/ Expression Operator Expression
/ Tinary
/ Lambda
/ Expression "." Identifier
/ Expression "[" Expression "]"
/ ArrayInitializer
Def < "def" Type? Identifier ("=" Expression)? ";"
Statement < (Expression ";"
/ If
/ For
/ While
/ Do
/ Foreach
/ Class
/ Function
/ Def ";"
/ Import ";"
/ Module ";")
Block < "{" Statement* "}" / Statement
If < "if" "(" Expression ")" Block
For < "for" "(" Def ";" Expression ";" Expression ")" Block
While < "while" "(" Expression ")" Block
Do < "do" Block "until" "(" Expression ")"
Foreach < "foreach" "(" (Variable ",")* Variable ";" Expression ")" Block
Lambda < "function" Type? "(" (Identifier ",")* Identifier? ")" Block
Function < "function" Type? Identifier "(" (Identifier ",")* Identifier? ")" Block
Import < "import" Identifier ("." Identifier)*
Module < "module" Identifier ("." Identifier)*
Type < "<" Identifier ">"
oop stuff (currently unused)
it compiles, but when i try to use it with
module main;
import metacode.parser;
import std.stdio;
string test = `
class test
{
function f()
{
}
}
`;
int main(string[] args)
{
ParseTree p = Metacode(test);
foreach(c;p.children)
{
writeln(c.name," ",c.children);
foreach(d;c.children)
{
writeln(d.name);
}
}
return 0;
}
and it crashes. why?
The text was updated successfully, but these errors were encountered: