I want to use a lexer and parser at their simplest level possible to solve the following problem
I have a file of which has content of the form
TYPE1 ABCR
{
TYPE2 EFG
{
omega 1
TYPE3 AFGH
{
alpha 1
beta 1 2
gamma 1 3 4
}
}
}
TYPE1 CFGRT
{
TYPE2 EFGI
{
omega 0
TYPE3 AFGHJ
{
beta 6 2
gamma 1 8 4
}
}
}
//.... (more members of TYPE1)
I have the following classes into which the above data can be parsed and populated
class TYPE1
{
List<TYPE2> listelems;
}
class TYPE2
{
omega abc;
List<TYPE3> listelems;
}
class TYPE3
{
vec2 beta
vec3 gamma
}
Now the specification of my file can get changed with say more attributes at every level
and also more hierarchy levels like TYPE4.
Thus I want to keep my parsing generic and extendable.
In this case if I were to use a lexer and parser like Flex/Bison what would be the language
grammar rules to be set for them?
Well, since there doesn’t appear to be any actual Flex/Bison users around right now and since you did say ‘…a lexer and parser like Flex/Bison…’, here’s how it would roughly look in the (proprietary) parser generator that I use:
This makes a few assumptions about what parts are reserved keywords, what parts are variable data, and so on, but you should get the idea.
identifierandinteger_literalare terminals.