Tuesday 7 June 2016

Flex and Bison

Continuing the habit of missing the whole day by about 5 or 10 minutes and then finding the time to write a post for the said day, here I am, attempting to write a compendium of my second day at training in the form of this blog post.

For most part of the day I wrestled with tutorials and books all for one singular purpose of learning flex and bison. And it's a little dissuading to admit that I have only started to fully comprehend the meaning and usage of flex and bison. The theoretical bit is everything one can ask for. It's interesting, logical, a little philosophical and most importantly, intuitively easy. 

Flex: It let's us define some patterns (regular expressions) and then scans through the given input to find every match there is to the defined patterns. These matching bits are called tokens. 
Bison: It let's us define some rules to which the tokens of the language must abide to. Relations and dependencies among tokens of various kinds, etc are described in this file. 

Without going into much details let me draw out how things work using a general sequence of events:

1) We define various token and their types in the bison file.
2) The bison file also include the grammar rules to be followed by these tokens.
3) The flex file describes using regular expressions how to identify these various tokens.
4) Then flex actually goes on to find these tokens in the input file. 
5) On receiving these tokens in such combinations as defined in the grammar rules, certain actions are triggered. These actions are basically C/C++ code written in either of the two files.
6) The whole process continues till the end of the file is reached.

I believe it is time now try and implement some these concepts to a project: SIM and learn further as needed. 

No comments:

Post a Comment