Skip to content

Instantly share code, notes, and snippets.

@yhirose
Last active September 16, 2016 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yhirose/1291a8ebc4da1d3858b67f980089b1a4 to your computer and use it in GitHub Desktop.
Save yhirose/1291a8ebc4da1d3858b67f980089b1a4 to your computer and use it in GitHub Desktop.
Expr ← Sum
Sum ← Product (('+' / '-') Product)*
Product ← Value (('*' / '/') Value)*
Value ← [0-9]+ / '(' Expr ')'
$ peglint calc2.peg
1:17 'Hello' is not defined.
# 構文ルール
Expr ← Sum
Sum ← Product (SumOpe Product)*
Product ← Value (ProOpe Value)*
Value ← Number / '(' Expr ')'
# 字句ルール
SumOpe ← ('+' / '-')
ProOpe ← ('*' / '/')
Number ← [0-9]+
$ go get github.com/yhirose/go-peg/cmd/peglint
$ peglint -s "2*(3+5)/4" -trace calc3.peg
pos:lev rule/ope
------- --------
0:0 [Expr]
0:1 reference
0:2 [Sum]
0:3 sequence
0:4 reference
0:5 [Product]
0:6 sequence
0:7 reference
0:8 [Value]
0:9 prioritizedChoice
0:10 reference
0:11 [Number]
0:12 oneOrMore
0:13 characterClass
1:13 characterClass
1:7 zeroOrMore
1:8 sequence
1:9 reference
1:10 [ProOpe]
1:11 prioritizedChoice
1:12 literalString
2:9 reference
2:10 [Value]
2:11 prioritizedChoice
[以下に続く...]
$ peglint calc.peg
Expr ← Sum Hello
Sum ← Product (('+' / '-') Product)*
Product ← Value (('*' / '/') Value)*
Value ← [0-9]+ / '(' Expr ')'
$ peglint -s "2*(3+5)/4" calc.peg
$ peglint -s "(hello world)" calc.peg
1:2 syntax error
$ peglint -s "2*(3+5)/4" -ast calc.peg
+ Expr
+ Sum
+ Product
+ Value
+ Value
+ Expr
+ Sum
+ Product
+ Value
+ Product
+ Value
+ Value
$ peglint -s "2*(3+5)/4" -ast -opt calc.peg
+ Product
+ Value
+ Sum
+ Value
+ Value
+ Value
~/Dropbox/Projects/go-peg-tutorial$ peglint -s "2*(3+5)/4" -ast -opt calc3.peg
+ Product
- Number ("2")
- ProOpe ("*")
+ Sum
- Number ("3")
- SumOpe ("+")
- Number ("5")
- ProOpe ("/")
- Number ("4")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment