Looking for LL(k) C grammar

"Italo Tasso" <italo@spwinternet.com.br>
Sat, 3 Nov 2007 09:51:13 -0200

          From comp.compilers

Related articles
Looking for LL(k) C grammar italo@spwinternet.com.br (Italo Tasso) (2007-11-03)
Re: Looking for LL(k) C grammar Ruslan@Shevchenko.Kiev.UA (rssh) (2007-11-05)
Re: Looking for LL(k) C grammar gneuner2@comcast.net (George Neuner) (2007-11-05)
| List of all articles for this month |

From: "Italo Tasso" <italo@spwinternet.com.br>
Newsgroups: comp.compilers
Date: Sat, 3 Nov 2007 09:51:13 -0200
Organization: Compilers Central
Keywords: C, LL(1), parse, question
Posted-Date: 05 Nov 2007 00:27:50 EST

I am looking for a LL(k) C grammar. Here is my problem:


I am trying to make a recursive descent parser for a simplified
version of C. It doesn't have to be predictive, but it would be good
to have no backup.


I started with the grammar in the K&R book. I removed some left
recursion and did some factorization. I was going bottom up in the
grammar, starting from the "constant" production. The problem is here:


assignment-expression: conditional-expression
| unary-expression assignment-operator assignment-expression
;


conditional-expression has unary-expression in it, so I must factorize it to
make it LL(k). But since there are so many productions between
conditional-expression and unary-expression, I just don't know how to do it.


I found some LL(2) C grammars with google, but they have some inconsistencies
when compared to the grammar in K&R.


I attached the grammar I wrote so far.


Thanks in advance.
exp : assignment_exp exp_sufix
        ;


exp_sufix : ',' assignment_exp exp_sufix
                    | empty
                    ;


assignment_exp : unary_exp assignment_operator assignment_exp
                              | conditional_exp
                              ;


assignment_operator : '=' | '*=' | '/=' | '%=' | '+=' | '-=' | '<<='
                                        | '>>=' | '&=' | '^=' | '|='
                                        ;


conditional_exp : logical_or_exp conditional_exp_sufix
                                ;


conditional_exp_sufix : '?' exp ':' conditional_exp
                                            | empty
                                            ;


const_exp : conditional_exp
                    ;


logical_or_exp : logical_and_exp logical_or_exp_sufix


logical_or_exp_sufix : '||' logical_and_exp logical_or_exp_sufix
                                          | empty
                                          ;


logical_and_exp : inclusive_or_exp logical_and_exp_sufix
                                ;


logical_and_exp_sufix : '&&' inclusive_or_exp logical_and_exp_sufix
                                            | empty
                                            ;


inclusive_or_exp : exclusive_or_exp inclusive_or_exp_sufix
                                  ;


inclusive_or_exp_sufix : '|' exclusive_or_exp inclusive_or_exp_sufix
                                              | empty
                                              ;


exclusive_or_exp : and_exp exclusive_or_exp_sufix
                                  ;


exclusive_or_exp_sufix : '^' and_exp exclusive_or_exp_sufix
                                              | empty
                                              ;


and_exp : equality_exp and_exp_sufix
                ;


and_exp_sufix : '&' equality_exp and_exp_sufix
                            | empty
                            ;


equality_exp : relational_exp equality_exp_sufix


equality_exp_sufix : '==' relational_exp equality_exp_sufix
                                      | '!=' relational_exp equality_exp_sufix
                                      | empty
                                      ;


relational_exp : shift_expression relational_exp_sufix


relational_exp_sufix : '<' shift_expression relational_exp_sufix
                                          | '>' shift_expression relational_exp_sufix
                                          | '<=' shift_expression relational_exp_sufix
                                          | '>=' shift_expression relational_exp_sufix
                                          | empty
                                          ;


shift_expression : additive_exp shift_expression_sufix
                                  ;


shift_expression_sufix : '<<' additive_exp shift_expression_sufix
                                              | '>>' additive_exp shift_expression_sufix
                                              | empty
                                              ;


additive_exp : mult_exp additive_exp_sufix
                          ;


additive_exp_sufix : '+' mult_exp additive_exp_sufix
                                      | '-' mult_exp additive_exp_sufix
                                      | empty
                                      ;


mult_exp : cast_exp mult_exp_sufix
                  ;


mult_exp_sufix : '*' cast_exp mult_exp_sufix
                              | '/' cast_exp mult_exp_sufix
                              | '%' cast_exp mult_exp_sufix
                              | empty
                              ;


cast_exp : unary_exp
                  ;


unary_exp : postfix_exp
                    | '++' unary_exp
                    | '--' unary_exp
                    | unary_operator cast_exp
                    ;


unary_operator : '&' | '*' | '+' | '-' | '~' | '!'
                              ;


postfix_exp : primary_exp postfix_exp_sufix
                        ;


postfix_exp_sufix : '[' exp ']' postfix_exp_sufix
                                    | '(' ')' postfix_exp_sufix
                                    | '(' argument_exp_list ')' postfix_exp_sufix
                                    | '.' id postfix_exp_sufix
                                    | '->' id postfix_exp_sufix
                                    | '++' postfix_exp_sufix
                                    | '--' postfix_exp_sufix
                                    | empty
                                    ;


primary_exp : id
                        | const
                        | '(' exp ')'
                        ;


argument_exp_list : assignment_exp argument_exp_list_sufix
                                    ;


argument_exp_list_sufix : ',' assignment_exp argument_exp_list_sufix
                                                | empty
                                                ;


const : int_const
            ;


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.