I don't think StackOverflow is the proper forum for a complete introduction to writing context-free grammars, but perhaps this is enough to get you started.
A grammar consists of a number of rules, each of which has the form "An X can be an a followed by a b followed by …". These rules can be recursive, and that is the only way to express concepts like arbitrary repetition. In other words, since we can't say "A list is any number of _expression_s separated by _Comma_s", what we say instead is: "A list can be an expression, or it can be a list followed by a Comma followed by an expression." We usually write that as follows:
list: expression /* A list can be an expression */
| list ',' expression /* or a list, a comma, and an expression */
The |
is just an abbreviation. We could have written:
list: expression /* A list can be an expression */
list: list ',' expression /* or a list, a comma, and an expression */
Note the use of ','
to represent "a comma". In bison, you don't have to think up names for tokens which consist of a single character; you can simply use the character itself in quotes. (In flex, to return that token, you do the same thing: { return ','; }
. That works because in C, a single-quoted character is an integer constant.)
For multicharacter tokens -- keywords like var
, for example -- bison lets you use a double-quoted string provided you have declared a token name. So for example, you could write:
/* Declare the token name and the string representation */
%token TOKEN_VAR "var"
%%
var_statement: "var" declaration_list ';'
declaration_list: declaration
| declaration_list ',' declaration
declaration: IDENTIFIER
| IDENTIFIER '=' expression
Now, a "program" is also a list of statements, but since the statements are terminated by semicolons, we don't need any punctuation in the list. That's a trivial difference:
program: statement
| program statement
statement: var_statement
| print_statement
| assignment_statement
There's a lot more to fill in here (expression
, for example), but hopefully that gives you some ideas.