Compiler/syntax analyzer: Difference between revisions

J: bugfix (use node display names rather than token names for operators)
(J: bugfix (use node display names rather than token names for operators))
Line 3,305:
 
<lang J>require'format/printf'
 
tkref=: tokenize 'End_of_input*/%+--<<=>>===!=!&&||print=print(if{else}while;,putc)a""0'
tkref,. (tknames)=: tknames=:;: {{)n
Line 3,314:
Identifier String Integer
}}-.LF
 
tkref,.(tktyp)=:tktyp=:;: {{)n
tkEOI tkMul tkDiv tkMod tkAdd tkSub tkNeg tkLss tkLeq tkGtr tkGeq tkEql
Line 3,320:
tkRbra tkWhile tkSemi tkComma tkPutc tkRpar tkId tkString tkInt
}}-.LF
 
tkV=: 2 (tkref i.tokenize '*/%+-<<=>>===!=&&||')} (#tktyp)#0
tkV=: 1 (1 0+tkref i.tokenize '-!')} tkV
Line 3,326:
tkPrec=: 14 (1 0+tkref i.tokenize'-!')} tkPrec
NB. proofread |:(<"1 tkV,.tkPrec),tktyp,tkref,:tknames
 
ndRef=: tkref
ndRef,.(ndDisp)=: ndDisp=:;:{{)n
Line 3,334:
}}-.LF
NB. proofread |:ndRef,:ndDisp
 
tok_pfx=: {{
if.'tok_'-:4{. y do. y else.'tok_',y end.
}}L:0
 
tok_s=: tok_pfx ;:'ln col name value text type valence precedence display'
tok=: {{
Line 3,345:
x{::~tok_s i. boxopen tok_pfx y
}}
 
gettoken=: {{
'tok_ln tok_col'=: (0;ndx){::x
Line 3,357:
tok_valence=: ind{::tkV
tok_precedence=: ind{::tkPrec
node_display=: ndDisp{::~tktyp i.<tok_type
ndx=:ndx+1
".each tok_s
}}
 
NB. syntax analyzer
 
parse=: {{
ndx=: tok_ln=: tok_col=: 0
Line 3,372 ⟶ 3,373:
end.
}}
 
stmt=:{{)v
t=. a:
Line 3,400 ⟶ 3,401:
'Print' expect tkSemi
case.tkSemi do.gettok''
case.tkId do.assert.
gettok v=. Identifier make_leaf tok_value
Assign expect tkAssign
Line 3,418 ⟶ 3,419:
t
}}
 
paren_expr=: {{
'paren_expr' expect tkLpar
Line 3,425 ⟶ 3,426:
t
}}
 
not_prec=: tkPrec{~tknames i.<Op_not
expr=: {{
Line 3,443 ⟶ 3,444:
end.
while.(2=tok_valence)*y<:tok_precedence do.
q=. tok_precedence [ op=. tok_typenode_display NB. no right associative operators
gettok''
e=. op make_node e expr q
Line 3,449 ⟶ 3,450:
e
}}
 
expect=: {{
if.y-:tok_type do. gettok'' return.end.
error '%s: Expecting "%s", found "%s"'sprintf x;(tkref{::~tktyp i.<y);tok_text
}}
 
make_leaf=: {{
x;y
}}
 
make_node=: {{
m;n;<y
}}
 
error=: {{
echo 'Error: line %d, column %d: %s\n'sprintf tok_ln;tok_col;y throw.
}}
 
 
syntax=: {{
;(flatAST parse y),each LF
}}
 
flatAST=: {{
assert.*L.y
Line 3,481 ⟶ 3,482:
end.
}}
 
</lang>
 
Line 3,539:
Integer 100
While
Less
tkLss
Identifier n
Identifier limit
Line 3,556:
Assign
Identifier n
Add
tkAdd
Identifier n
Integer 2
While
And
tkAnd
LessEqual
tkLeq
Multiply
tkMul
Identifier k
Identifier k
Line 3,572:
Assign
Identifier p
NotEqual
tkNeq
Divide
tkDiv
Identifier n
Multiply
tkMul
Identifier k
Identifier k
Line 3,581:
Assign
Identifier k
Add
tkAdd
Identifier k
Integer 2
Line 3,601:
Assign
Identifier count
Add
tkAdd
Identifier count
Integer 1
6,951

edits