Comments (5)
Hi @joagre,
I noticed two small problems:
- the negative matches should be done before you parse
Variable
orFunctionCall
true
andfalse
can be parsed as variable name, which breaks some type checks
If you change it like this, it should be much better:
NonArithmetic <- !NumberLiteral (Variable / FunctionCall)
NonLogical <- !BooleanLiteral (Variable / FunctionCall)
Variable <- !BooleanLiteral [a-zA-Z_][a-zA-Z0-9_]*
from packcc.
Melts my brain. :-)
I know that feeling, it happens often with peg grammars :-)
I strongly suggest to add PCC_DEBUG macro to your grammar. It will show you what is happening inside, which makes it much easier to locate the problems. Basic implementation can look like this:
%earlysource {
static const char *dbg_str[] = { "Evaluating rule", "Matched rule", "Abandoning rule" };
#define PCC_DEBUG(auxil, event, rule, level, pos, buffer, length) \
fprintf(stderr, "%*s%s %s @%zu [%.*s]\n", (int)((level) * 2), "", dbg_str[event], rule, pos, (int)(length), buffer)
}
More details can be found in README.md section on macros.
Now to the problem with a*1
: It fails, because it parses a
as a valid logical expression and then it doesn't know what to do with the rest. Unfortunately, this is not easy to fix within you grammar. At least I don't see any easy way to do it. You could enforce that logical and arithmetic expressions must end with ;
, which would solve this particular case. But that would mean you than would have tu use semicolon in FunctionCall as well, which you probably don't want to.
You might also try this:
LogicalExpr <- OrExpr &[;,)]
ArithmeticExpr <- AdditiveExpr &[;,)]
This should ensure that the expression is always parsed in full. It might cause you some headache later though :-)
Also I think you are missing parentheses in Statement
:
Statement <- (Assignment / Expression) ';'
Unless it is your intention to allow assignments not followed by semicolon - which would break the suggestion above.
from packcc.
Thanks for the input! Invaluable. It works much better now but a*1;
is still invalid. A few tests that show the wanted behavior:
$ echo "1+1;" | ./foo
$ echo "true&&false;" | ./foo
$ echo "1+a;" | ./foo
$ echo "true&&1;" | ./foo
Syntax error
$ echo "1&&true;" | ./foo
Syntax error
$ echo "true+1;" | ./foo
Syntax error
$ echo "1*a;" | ./foo
and then the culprit:
$ echo "a*1;" | ./foo
Syntax error
Melts my brain. :-)
Thanks!
/J
from packcc.
New version:
%prefix "test"
Program <- Statement+
Statement <- Assignment / Expression ';'
Assignment <- Variable '=' Expression
# Expressions
Expression <- LogicalExpr / ArithmeticExpr
# Logical expressions
LogicalExpr <- OrExpr
OrExpr <- AndExpr ('||' AndExpr)*
AndExpr <- NotExpr ('&&' NotExpr)*
NotExpr <- '!' LogicalPrimary / LogicalPrimary
LogicalPrimary <- BooleanLiteral / NonArithmetic / Variable / FunctionCall
NonArithmetic <- !NumberLiteral (Variable / FunctionCall)
# Arithmetic expressions
ArithmeticExpr <- AdditiveExpr
AdditiveExpr <- MultiplicativeExpr (('+' / '-') MultiplicativeExpr)*
MultiplicativeExpr <- UnaryExpr (('*' / '/') UnaryExpr)*
UnaryExpr <- ('+' / '-')? ArithmeticPrimary
ArithmeticPrimary <- NumberLiteral / NonLogical / Variable/ FunctionCall
NonLogical <- !BooleanLiteral (Variable / FunctionCall)
# Handling of literals and variables
NumberLiteral <- [0-9]+ ('.' [0-9]+)?
BooleanLiteral <- 'true' / 'false'
Variable <- !BooleanLiteral [a-zA-Z_][a-zA-Z0-9_]*
FunctionCall <- Variable '(' (Expression (',' Expression)*)? ')'
%%
int main() {
test_context_t *context = test_create(NULL);
test_parse(context, NULL);
test_destroy(context);
return 0;
}
from packcc.
The PCC_DEBUG macro gave me a better understanding about PEG parser's way of working. I was on a failed cause. I'll move the type checking alltogether to Hindley Milner.
Thanks
/J
from packcc.
Related Issues (20)
- Lexical state support HOT 5
- Import support HOT 10
- Questions not answered in the README HOT 12
- Passing auxil to PCC_DEBUG HOT 3
- AST Mode HOT 11
- Uninitialized variables HOT 8
- value passthrough HOT 4
- parser reads more data than necessary HOT 3
- Strange bad generated parser HOT 5
- Memory usage compared to gcc compiler HOT 13
- raising error from action? HOT 2
- %value and memory management HOT 2
- Consider add support for UCD(Unicode Character Database) rule pattern HOT 3
- Lookahead woes and more HOT 2
- Parsing a "switch { case n: m }"
- How to do good syntax error handling? HOT 2
- Null pointer error on broken grammar HOT 2
- parsing python like languages HOT 4
- Actions on repeated rules HOT 2
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 packcc.