Docsity

Understanding Variables, Instance Variables, and Semantic Errors in Programming, Exams of Computer Science

Partial preview of the text

Download Understanding Variables, Instance Variables, and Semantic Errors in Programming and more Exams Computer Science in PDF only on Docsity!

Compilers CS414-2008-FRFinal Review

David Galles Department of Computer ScienceUniversity of San Francisco

FR-0:

Deterministic Finite Automata Set of states Initial State Final State(s) Transitions

DFA for else, end, identifiersCombine DFA

FR-2:

Automatic Creation of DFAs

We’d like a tool:

Describe the tokens in the language Automatically create DFA for tokens Then, automatically create C code that implementsthe DFA We need a method for describing tokens

FR-3:

Formal Languages Alphabet

Σ: Set of all possible symbols

(characters) in the input file

Think of

Σ^

as the set of symbols on the

keyboard String

w: Sequence of symbols from an alphabet String length

|w

|^ Number of characters in a

string:

|car

|^ = 3,

|abba

|^ = 4

Empty String

ǫ: String of length 0:

|ǫ|

Formal Language

: Set of strings over an

alphabet Formal Language

6 =^

Programming language – Formal

Language is only a set of strings.

FR-5:

Language Concatenation Language Concatenation

Given two formal

languages

L^1

and

L^2

, the concatenation of

L^1

and

L,^2

L^1

L^2

=^ {

xy|

x^ ∈

L^1

, y^

∈^ L

For example:{fire, truck, car} {car, dog} ={firecar, firedog, truckcar, truckdog, carcar, cardog}

FR-6:

Kleene Closure

Given a formal language

L:

0 L

=^

{ǫ} (^1) L =^

L

2 L

=^

LL

3 L

=^

LLL

4 L

=^

LLLL∗^ L=

0 L

1 L

⋃^

2 L

⋃^

...^

⋃^

n^ L

⋃^

FR-8:

r.e. Precedence

From highest to Lowest:Kleene Closure *ConcatenationAlternation

ab*c

|e = (a(b*)c)

|^ e

FR-9:

Regular Expression Examples

(a|b)*

all strings over {a,b}

binary integers (with leading zeroes)

a(a

|b)*a

all strings over {a,b} thatbegin and end with a

(a|b)*aa(a

|b)*

all strings over {a,b} thatcontain aa

b(abb)*(a

|ǫ)^

all strings over {a,b} thatdo not contain aa

FR-11:

r.e. Shorthand Examples Regular Expression

Language if^ {if}

["a"-"z"]["0"-"9","a"-"z"]*

Set of legal identifiers

["0"-"9"]+

Set of integers (with leading zero

(["0"-"9"]+"."["0"-"9"]*)

|^ Set of real numbers

(["0"-"9"]*"."["0"-"9"]+)

FR-12:

Parsing Once we have broken an input file into a sequenceof tokens, the next step is to determine if thatsequence of tokens forms a syntactically correctprogram – parsing We will use a tool to create a parser – just like weused lex to create a parser We need a way to describe syntactically correctprograms^ Context-Free Grammars

FR-14:

Generating Strings with CFGs Start with the initial symbol Repeat:^ Pick any non-terminal in the string^ Replace that non-terminal with the right-handside of some rule that has that non-terminal asa left-hand sideUntil all elements in the string are terminals

FR-15:

CFG Example

E^ →

E^

+^ E

E^ →

E^

−^ E

E^ →

E^

∗^ E

E^ →

E/E

E^ →

num

FR-17:

Ambiguous Grammars A Grammar is

ambiguous

if there is at least one

string with more than one parse tree The expression grammar we’ve seen so far isambiguous E^ →

E^

+^ E

E^ →

E^

−^ E

E^ →

E^

∗^ E

E^ →

E/E

E^ →

num

FR-18:

Expression Grammar

E^ →

E^

+^ T

E^ →

E^

−^ T

E^ →

T

T^ →

T^

∗^ F

T^ →

T /F

T^ →

F

F^ →

num F^ →

(E