0
votes

I am trying to write compiler using Flex, Bison which takes file as input. File contains description of algorithm behavior. File has 2 sections.

1. definition of registers, states
2. description of behavior

Example of input file

registers = {state, temp,input}
states = {idle, done}

entity
    begin
        read(input)
        temp = input
        send(temp)
        become done
    end

My problem is following
File is divided into two sections. I need to find out if input which is analyzed belongs to correct section. For example definition of registers registers = {...} should not be placed in definition of behavior for entity.

I came up with these solutions.
1. Wrap regular expression in lex with expression at the beginning and the end. For example section with behavior starts with begin and ends with end. So basically if I define regular expression like "begin.become.end" it should parse correctly command become only when it is situated between begin and end.
2. Define separate flex file with expressions for every section. Afterwards read input file and parse it with one flex file with expressions and after compiler gets to for example keyword begin it will switch flex file for file where are expressions for parsing behavior.

My questions is if either my proposed solutions are a good way to approach problem or there is more elegant and correct way for solving problems of this type.

Thanks.

1
@downvoter Please explain.user207421

1 Answers

2
votes

No. You don't need to mess around with the lexer for this. Just define it all in the grammar. That's what it's for.

program
    : registers states entities
    ;

registers
    : REGISTERS '{' register_list '}'
    ;

register_list
    : register
    | register_list ',' register
    ;

// similarly for states

entities
    : entity
    | entities entity
    ;

entity
    : ENTITY BEGIN entity_stuff END
    ;

// etc, whatever entity_stuff can be, not including 'registers' or 'states'.

If anything appears in the wrong section it will cause a syntax error.

You might want to allow registers and states to appear in either order, if it makes sense, or to be absent or empty (ditto). Left as an exercise for the reader.