Description
Introduction
The parser’s job is to figure out the relationship(grammar) among the input tokens. Those tokens come from the scanner (flex).
A grammar is a series of rules that the parser uses to recognize syntactically valid input.
Bison only deal with the syntax no the semantic, for instance the code bellow has a valid syntax but is invalid, bison will think that this is fine
int var = "Leo"
Consider the following grammar on bison. NAME, NUMBER, '+', '-', are tokens that flex gives to us.
statement: NAME '=' expression
expression: NUMBER '+' NUMBER
| NUMBER '−' NUMBER
Every line here is a rule, so the rule for a statement is the pattern "NAME = expression". And the rule for expression is the pattern "NUMBER+NUMBER" OR "NUMBER-NUMBER"
Every grammar includes a start symbol, the one that has to be at the root of the parse tree. In this grammar, statement is the start symbol.
So the following source code line:
fred = 12 + 13
Would produce from the scanner the following tokens:
NAME,'=',NUMBER,'+',NUMBER
This AST tree would be created:
Where "fred =" is a statement. And "12+13" is an expression.
Now what would happen if we would want to parse a longer instruction
fred = 14 + 23 − 11 + 7
To solve this can use a Recursive rule, where the rule call itself.
statement: NAME '=' expression
expression: NUMBER
| expression '+' NUMBER
| expression '−' NUMBER
Now the tree would grow to this:
To start use our token inputs, starting from a simple example.
function result = justSum(a,b)
result = a + b; # Simple comment
end
Tokens generated with out scanner
FUNCTION: function
Identifier: result
ASSIGN: =
Identifier: justSum
Open Parenthesis: (
Identifier: a
Comma: ,
Identifier: b
Close Parenthesis: )
Identifier: result
ASSIGN: =
Identifier: a
Plus: +
Identifier: b
EndLine: ;
END: end
And build an AST tree
Class diagram for parser Nodes
Considering the class diagram bellow, we need to create a rule for every class that inherites from Node.
References
- https://www.youtube.com/watch?v=oOFfVZrvhMA
- https://www.youtube.com/watch?v=yTXCPGAD3SQ
- http://aquamentus.com/flex_bison.html
- https://github.com/meyerd/flex-bison-example
- https://www.usna.edu/Users/cs/lmcdowel/courses/si413/F10/labs/L04/calc1/ex1.html
- https://learn.yancyparedes.net/2013/03/writing-a-calculator-program-using-bison-and-flex/
- calculator.pdf
- http://www.progtools.org/compilers/tutorials/cxx_and_bison/cxx_and_bison.html
- http://stackoverflow.com/questions/2993658/creating-simple-calculator-with-bison-flex-in-c-not-c
- https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form
- https://vinaytech.wordpress.com/2008/10/04/abstract-syntax-tree/
- Matlab parser and scanner on matlab
- Matlab Parser final project.pdf
- http://stackoverflow.com/questions/9583307/where-can-i-find-a-formal-grammar-for-matlab
- Language Implementation Patterns.pdf
Activity
Add diagram for our parser, Issue #3
[-]Start parser with our token inputs[/-][+]Start parser with our token inputs with Bison[/+]leonardoaraujosantos commentedon Sep 18, 2016
BNF
Backus Normal Form is one notation techniques for context-free grammars, often used to describe the syntax of programming languages like C, or Python
Example:

This translates into English as:
On Bison
Read as ”stmt is a var_decl or func_decl or expr” and if this grammar match execute something on the ”action block”. Inside the action block we mount our AST Tree
This would create the grammar for the if statement, like if (condition) block, or if (condition)