Is it possible to parse a C program using LL (1) parser?

    1 answer 1

    Not.

    Grammar C is not context free. For example, the expression

    a((b)*c); 

    may be

    • if a is a function, b is a type, c is a pointer: dereferencing the pointer c , casting the result to type b , passing the resulting value as a parameter to the function a ;
    • if a is a function, b is a numeric variable / constant, c is a numeric variable / constant: multiplying the values ​​of b and c , passing the value obtained as a parameter to the function a .

    Thus, the correct analysis is impossible without context. LL parsers are intended only for context-free grammars.

    • And what kind of parser would you recommend using? - PaulD
    • I would take lex / yacc (flex / bison), enter explicit context, resolve ambiguities manually. - VladD
    • @SoloMio Here is the grammar for ANTLR 4 github.com/antlr/grammars-v4/blob/master/c/C.g4 - andrybak
    • one
      @Andrey: typedefName: Identifier; Something I do not think this grammar is unambiguous. How does she distinguish two cases from my answer? - VladD
    • @VladD, and what is meant by an explicit context? - PaulD