Redo PL/0 parser using ebnf

release
Chris Double 2007-11-28 16:07:23 +13:00
parent e0adc1a7fa
commit d3ac10aefc
2 changed files with 20 additions and 49 deletions

View File

@ -27,12 +27,12 @@ GENERIC: ebnf-compile ( ast -- quot )
M: ebnf-terminal ebnf-compile ( ast -- quot )
[
ebnf-terminal-symbol , \ token ,
ebnf-terminal-symbol , \ token , \ sp ,
] [ ] make ;
M: ebnf-non-terminal ebnf-compile ( ast -- quot )
[
[ ebnf-non-terminal-symbol , \ search , \ execute , ] [ ] make
[ ebnf-non-terminal-symbol , \ search , \ execute , \ sp , ] [ ] make
, \ delay ,
] [ ] make ;

View File

@ -1,58 +1,29 @@
! Copyright (C) 2007 Chris Double.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel arrays strings math.parser sequences peg ;
USING: kernel arrays strings math.parser sequences peg peg.ebnf ;
IN: peg.pl0
#! Grammar for PL/0 based on http://en.wikipedia.org/wiki/PL/0
: 'ident' ( -- parser )
: ident ( -- parser )
CHAR: a CHAR: z range
CHAR: A CHAR: Z range 2array choice repeat1
[ >string ] action ;
: 'number' ( -- parser )
: number ( -- parser )
CHAR: 0 CHAR: 9 range repeat1 [ string>number ] action ;
DEFER: 'factor'
: 'term' ( -- parser )
'factor' "*" token "/" token 2array choice sp 'factor' sp 2array seq repeat0 2array seq ;
: 'expression' ( -- parser )
[ "+" token "-" token 2array choice sp optional 'term' sp 2dup 2array seq repeat0 3array seq ] delay ;
: 'factor' ( -- parser )
'ident' 'number' "(" token hide 'expression' sp ")" token sp hide 3array seq 3array choice ;
: 'condition' ( -- parser )
"odd" token 'expression' sp 2array seq
'expression' { "=" "#" "<=" "<" ">=" ">" } [ token ] map choice sp 'expression' sp 3array seq
2array choice ;
: 'statement' ( -- parser )
[
'ident' ":=" token sp 'expression' sp 3array seq
"call" token 'ident' sp 2array seq
"begin" token 'statement' sp ";" token sp 'statement' sp 2array seq repeat0 "end" token sp 4array seq
"if" token 'condition' sp "then" token sp 'statement' sp 4array seq
4array choice
"while" token 'condition' sp "do" token sp 'statement' sp 4array seq
2array choice optional
] delay ;
: 'block' ( -- parser )
[
"const" token 'ident' sp "=" token sp 'number' sp 4array seq
"," token sp 'ident' sp "=" token sp 'number' sp 4array seq repeat0
";" token sp 3array seq optional
"var" token 'ident' sp "," token sp 'ident' sp 2array seq repeat0
";" token sp 4array seq optional
"procedure" token 'ident' sp ";" token sp 'block' sp 4array seq ";" token sp 2array seq repeat0 'statement' sp 2array seq
3array seq
] delay ;
: 'program' ( -- parser )
'block' "." token sp 2array seq ;
<EBNF
program = block '.' .
block = [ 'const' ident '=' number { ',' ident '=' number } ';' ]
[ 'var' ident { ',' ident } ';' ]
{ 'procedure' ident ';' [ block ';' ] } statement .
statement = [ ident ':=' expression | 'call' ident |
'begin' statement {';' statement } 'end' |
'if' condition 'then' statement |
'while' condition 'do' statement ] .
condition = 'odd' expression |
expression ('=' | '#' | '<=' | '<' | '>=' | '>') expression .
expression = ['+' | '-'] term {('+' | '-') term } .
term = factor {('*' | '/') factor } .
factor = ident | number | '(' expression ')'
EBNF>