Related articles |
---|
Newbie parsing a simple bespoke language questions chris.morley@lineone.net (Chris Morley) (2007-12-10) |
Re: Newbie parsing a simple bespoke language questions chris.morley@lineone.net (Chris Morley) (2007-12-12) |
From: | "Chris Morley" <chris.morley@lineone.net> |
Newsgroups: | comp.compilers |
Date: | Mon, 10 Dec 2007 22:34:47 -0000 |
Organization: | Zen Internet |
Keywords: | parse, question |
Posted-Date: | 10 Dec 2007 20:35:33 EST |
Hi,
I am building a parser for a simple bespoke script like interpreter
which has very basic syntax & structure and a very limited
context/state held during parsing. My parser is in C++ using bison
2.3. Most commands are of the type:
block destination {
mycommand par1, par2,par3 /flag1 /flag2
mycommand2 ...
...
}
block destination {...}
EOF
I'm sure you get the drift!
I've done quite a bit of reading on the internet and can see two ways to
check the semantics of each statement (ensure pars are of correct type &
flags are appropriate).
1) produce a tree (probably just a stack is enough as it is so simple) and
check the semantics in some C++ code after generating the tree.
e.g.
command: COMMAND params flags { somecommand($1,$2,$3) };
params: /* nothing */
| params ',' param { push($3) };
etc. where params and flags are a stack or vector &
somecommand(ID,pars,flags) checks the types. And have one line in the
grammar for all commands.
2) put the semantics in the Bison source & have lines in the grammar for
each command
e.g.
commands: c_one {}
| c_two {}
| etc...
c_one: "command1" PAR_INTEGER ',' PAR_FLOAT flags { command1($2, $4,$5);}
c_two: "command2" PAR_INTEGER ',' PAR_INTEGER flags { command2($2, $4,$5);}
...
I can't see a clincher either way in the pros and cons! I'd appreciate some
input before I head off doing it all one way and finding a huge D'OH! moment
and have to change it all!
Thanks,
Chris
[My advice is invariably to have the parser accept a larger language
and do the type checking in your semantic code. That lets you produce
useful error messages like "foo command requires two integers" rather
than a generic syntax error. -John]
Return to the
comp.compilers page.
Search the
comp.compilers archives again.