Finish Parsing




CS152

Chris Pollett

Sep 20, 2021

Outline

Introduction

Writing LL(1) Grammars

Eliminating Left Recursion and Common Prefixes

Ambiguity and the Dangling Else Problem

Solving the Dangling Else Problem

Quiz

Which of the following statements is true?

  1. Whitespace doesn't matter for the make utility.
  2. There exists a regular expression that cannot be converted to an NFA.
  3. LL stands for left-to-right leftmost derivation.

Table-Driven Top-Down Parsing

Table-driven Parser Driver Table-driven Parser Parse Table

Generating Parse and Production Tables From a Grammar

More on Predictive Parsing

Second Criteria for Predictive Parsing

First, Follow for Calculator Example

First and Follow sets for calculator example

Tools for Scanners and Parsers.

A Simple Lex Example

%{
#include <stdio.h>
int wordCount = 0;
%}
word [^ \t\n]+ /* make an abbreviation word for the expr 
    [^ \t\n]+ (make sure to delete this comment before runninf lex)*/
%%
[\t\n ]+ {printf("I see whitespace\n");} //what to do if see pattern
{word} {wordCount++;}
%%
int main()
{
   yylex(); //call the lexer. Gets input from command line until ^D
   printf("word count: %d", wordCount); return 0;
}
To compile:
lex  -o lextest.c lextest.l #default output is lex.yy.c
gcc  lextest.c -o lextest -ll  #-ll not needed if use flex.

A Yacc Example

Suppose we put this file in yacctest.y

%{
#include <stdio.h>
int yylex();
int yyerror();
%}
%token ARTICLE NORMAL_NOUN PROPER_NOUN
%%
noun_phrase : PROPER_NOUN { printf("Proper Noun\n"); }
    | ARTICLE NORMAL_NOUN {printf("Usual Noun\n"); }
%%
int main(int argc, char **argv)
{
  extern FILE *yyin;
  yyin = fopen(argv[1], "r"); //sets up lexer to use this file as input
  yyparse();  
  fclose(yyin);  
}

More on Yacc Example

Still More on Yacc Example