Comments (2)
Sorry for the late reply.
An error state should be introduced as a part of the value type, which is specified using %value
.
I show an example below. Only zero-division error is implemented here.
%prefix "calc"
%value "calc_result_t"
%header {
typedef struct calc_result_tag {
int value;
int error; /* 0: no error, 1 error. */
} calc_result_t;
}
%source {
#include <stdio.h>
#include <stdlib.h>
static calc_result_t calc_result_new(int value, int error) {
calc_result_t r;
r.value = value;
r.error = error;
return r;
}
}
statement <- _ e:expression _ EOL { if (e.error) printf("arithmetic error\n"); else printf("answer=%d\n", e); }
/ ( !EOL . )* EOL { printf("syntax error\n"); }
expression <- e:term { $$ = e; }
term <- l:term _ '+' _ r:factor { $$ = calc_result_new(l.value + r.value, l.error | r.error); }
/ l:term _ '-' _ r:factor { $$ = calc_result_new(l.value - r.value, l.error | r.error); }
/ e:factor { $$ = e; }
factor <- l:factor _ '*' _ r:unary { $$ = calc_result_new(l.value * r.value, l.error | r.error); }
/ l:factor _ '/' _ r:unary { $$ = (r.value != 0) ? calc_result_new(l.value / r.value, l.error | r.error) : calc_result_new(0, 1); }
/ e:unary { $$ = e; }
unary <- '+' _ e:unary { $$ = e; }
/ '-' _ e:unary { $$ = calc_result_new(-e.value, e.error); }
/ e:primary { $$ = e; }
primary <- < [0-9]+ > { $$ = calc_result_new(atoi($1), 0); }
/ '(' _ e:expression _ ')' { $$ = e; }
_ <- [ \t]*
EOL <- '\n' / '\r\n' / '\r' / ';'
%%
int main() {
calc_context_t *ctx = calc_create(NULL);
while (calc_parse(ctx, NULL));
calc_destroy(ctx);
return 0;
}
from packcc.
thank you very much, that answers my question and is a great example. thank you so much for taking the time to enlighten me! you can close this issue.
from packcc.
Related Issues (20)
- [Bug] Conflict caused by FALSE, TRUE macros and bool_tag enum using same name HOT 7
- Actions that runs before the end of parsing. HOT 8
- Simple grammar goes into an infinite loop instead of erroring HOT 1
- `$n` should be independent across alternatives HOT 4
- C predicates HOT 2
- Peg only mode? HOT 9
- Parser very slow with repeated parse calls HOT 3
- 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
- %value and memory management HOT 2
- Consider add support for UCD(Unicode Character Database) rule pattern HOT 3
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.