Dana Vrajitoru
C311 Programming Languages

Compiler Compilers

Compiler Compilers

Lex

Example in Lex

%%
[ \t]+$	;
[ \t]+ 	printf(" ");
  • %% is the beginning of the set of rules,
  • [ ]+ means one or more of the content (the Kleene +)
  • $ means the end of the line. \t is any whitespace.
  • Every input character that is not matched by a rule is copied to the output as is.
  • The output replaces any sequence of whitespaces with a single space, except at the end of a line where they are deleted.

    Yacc and Lex

           lexical      grammar
            rules        rules
              ||          ||
              \/          \/
             Lex         Yacc
              ||          ||
              \/          \/
    Input -> yylex  ->  yyparse -> Parsed input
    
  • Lex is used for the lexical analysis of the program, while Yacc is used for the parsing step.

    Source File Format
    Lex:
    definitions
    %%
    rules
    %%
    user subroutines      
    Yacc:
    declarations
    %%
    rules
    %%
    programs

    Yacc Syntax

    Examples of Rules

    Example

    %left '+' '-' 
    %left '*' '/' 
    %% 
    expr : expr '+' expr | 
           expr '-' expr | 
           expr '*' expr | 
           expr '/' expr | 
           '-' expr %prec '*' | 
           NAME ;