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 ) M: ebnf-terminal ebnf-compile ( ast -- quot )
[ [
ebnf-terminal-symbol , \ token , ebnf-terminal-symbol , \ token , \ sp ,
] [ ] make ; ] [ ] make ;
M: ebnf-non-terminal ebnf-compile ( ast -- quot ) M: ebnf-non-terminal ebnf-compile ( ast -- quot )
[ [
[ ebnf-non-terminal-symbol , \ search , \ execute , ] [ ] make [ ebnf-non-terminal-symbol , \ search , \ execute , \ sp , ] [ ] make
, \ delay , , \ delay ,
] [ ] make ; ] [ ] make ;

View File

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