1.0 Languages, Expressions, Automata
Alphabet: a finite set, typically a set of symbols.
Language: a particular subset of the strings that can be made from the alphabet.
ex:
an alphabet of digits = cfw_-,0,1,2,3,4,5,6,7,8,9
a language of integers = cfw_0,
Lexical Analysis
Here is an overview of a compiler:
High level
Language
Program
Machine
Language
Program
Intermediate
Representation
Front end: Analysis
Scanner
Parser
Back end: Synthesis
Intermediate Code
Generation
Optimization
Code Generation
Semantic
Handout #2
EXERCISES ON RECURSION
Solve the following problems recursively:
1.
Count the number of zeros in an array.
2.
Print out an array.
3.
Print out the data in a linked list forward.
4.
Print out the data in a linked list backward.
5.
Convert a deci
Turing Machines
Previously we discussed increasingly complex machines to accept languages. The
power was increased by providing additional storage. Specifically:
FAs could only remember via their states
PDAs were FAs to which we added a stack which is o
Syntactic Analysis, Recursive Descent Parsing
Syntactic Analysis/Parsing
The parser is the second phase of a compiler, it is significantly more complex than
the scanner. It assembles the tokens into constructs (e.g. statements, expressions,
program, block
Handout # 12
CSC 135
SOLUTIONS - EXERCISES ON PROLOG
1. Define a third predicate so that third(X, Y) says that Y is the third element of the list X. The
predicate should fail if X has fewer than three elements. Hint: this can be expressed as a fact.
third
import java.io.*;
import java.util.Scanner;
/-/ Recognizer for simple expression grammar
/ Written by Scott Gordon 1/03
/ minor modifications made 2/04, 9/06, 2/09, 9/11, 2/15
/
/
/ to run on Athena (linux) / save as: Recognizer.java
/ compile: javac Reco
Handout # 9A
CSC 135
SOLUTIONS - EXERCISES ON PUMPING LEMMA
Using the pumping Lemma, prove that the following languages are not regular:
1.
L = cfw_anb2n
Assume L is regular. From the pumping lemma there exists a p such that every w L such that |w| p
can
Handout # 3
CSC 135
EXERCISES ON REGULAR EXPRESSIONS AND FINITE AUTOMATA
Give regular expressions representing the following languages on cfw_a, b or cfw_0, 1:
1. all stringsstrings which begin and end with the same letter
3. strings which begin and end w
Languages, Automata, Grammars, and Regular Expressions
Note: some of this is material you should have seen in CSc 28 (or its equivalent).
Languages, automata and grammars (regular expressions although not technically
grammars function are substitutes for
Sample Scheme Exercises - Solutions
1. Draw box diagrams for the following scheme lists:
a. ( ( ( ( a ) ) ) )
NIL
NIL
NIL
NIL
a
b. ( 1 ( 2 ( 3 4 ) ) ( 5 ) )
NIL
1
5
NIL
NIL
2
3
4
NIL
c. ( ( a ( ) ) ( ( c ) ( d ) b ) e )
e
NIL
b
NIL
a
NIL
NIL
c
NIL
d
NIL
N
CSc 135 Fall 2016 - Programming Languages
Homework 2
due Wednesday November 2 at 11:59:00 pm
Write the following Scheme functions:
1. "digitinc4" -takes as input a 4-digit integer, and returns another 4-digit integer constructed
of the original input inte
Handout # 4
CSC 135
EXERCISES ON LEXICAL ANALYSIS
Imagine you have a language that has only operators, identifiers, and two reserved words, and the following
characteristics:
the only legal characters are the letters A and B, the digit 1, and the charact
Logic Programming and Prolog
See Handout # 11 on Logic History
Logic has a long standing history going back to the Greek philosophers. Aristotle is
regarded as the father of logic. He devised systematic methods for analyzing and
evaluating arguments. He d
Evolution of Programming Languages
Evolution has been influenced by a number of factors, among which are:
computer architecture: the Von Neuman model where the CPU is separate
from the memory. Both program and data co-exist in memory. Languages
based on
Handout # 12
CSC 135
EXERCISES ON PROLOG
1. Define a third predicate so that third(X, Y) says that Y is the third element of the list X. The
predicate should fail if X has fewer than three elements. Hint: this can be expressed as a fact.
2. Define a first
2.0 Formal Grammars
Context-Free Grammar (CFG). A language generator consisting of:
1.
2.
3.
4.
a set T of terminals (usually denoted with lowercase letters)
a set N of non-terminals (usually denoted with uppercase letters)
a unique start symbol S 0 N
a s
3.0 Lexical Analysis
First, lets see a simplified overview of the compilation process:
Step 2: parsing
(syntax analysis)
source code file
(sequence of char)
Step 1: scanning
(lexical analysis)
Tokenized version
(sequence of tokens)
Parse Tree
Step 3:
code
5.0 EBNF (Extended BNF) and Syntax Diagrams
EBNF is the same as BNF, with three additional meta-symbols:
cfw_ which indicates 0 or more
[ ] which indicates optional
( | | ) which indicates sub-alternatives
EBNF has exactly the same expressive power as
6.0 Recursive-Descent Parsing
Building a simple recognizer:
1. Convert BNF grammar to EBNF and syntax diagrams.
2. There must be an identifier token that points to the current token.
3. There must be a function match that:
a. tests whether current token i
7.0 Non-Deterministic Finite Automata (NFA)
A non-deterministic finite automaton is one that (1) allows multiple arcs with
the same label to exit a node, and (2) allows arcs with a label. It is also
customary in an NFA to relax the completeness constraint
8.0 Regular Grammars and Regular Languages
Recall the general form of regular expressions:
If A is an alphabet, and a A , then a is a regular expression.
is a regular expression.
If r and s are regular expressions, then the following are also regular
exp
The
Chomsky
Hierarchy
Formal Grammars,
Languages, and the
Chomsky-Schtzenberger
Hierarchy
Overview
01 Personalities
02 Grammars and languages
03 The Chomsky hierarchy
04 Conclusion
Personalities
Noam Chomsky
Marcel Schtzenberger
Others.
01
Noam Chomsky
Bo
Handout # 13
CSC 135
SOLUTIONS - EXERCISES ON TURING MACHINES
1. Construct a Turing Machine which accepts the language
L = cfw_ w | w cfw_a, b* & |w| is a positive multiple of 3.
a. Give a description, in words, of your approach in designing the machine.