Docsity

CS360: Programming Language Concepts - Language Specification & Syntax Analysis, Study notes of Programming Languages

Toggle sidebar
bg1
1
4/7/2005
1
Welcome to CS360
Programming Language
Concepts
Dr. Amie Souter
Announcements
• Assignment 1
• Mailing list
• Reading: Chapter 4 (1-2) and 11 (1-3)
4/7/2005
3
Exercise
• Given the following program, write
down a description of the syntax for
the program.
• Questions:
– 1. Is this a difficult task?
– 2. What would make it easier?
– 3. How are languages typically specified?
4/7/2005
4
Class this Week
•How do we specify tokens?
• Recognizing a token
• How do we specify grammars?
• Using grammars
• Functional languages
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download CS360: Programming Language Concepts - Language Specification & Syntax Analysis and more Study notes Programming Languages in PDF only on Docsity!

4/7/2005 1

Welcome to CS

Programming Language

Concepts

Dr. Amie Souter

Announcements

  • Assignment 1
  • Mailing list
  • Reading: Chapter 4 (1-2) and 11 (1-3)

4/7/2005 3

Exercise

  • Given the following program, write

down a description of the syntax for

the program.

  • Questions:
      1. Is this a difficult task?
      1. What would make it easier?
      1. How are languages typically specified?

4/7/2005 4

Class this Week

  • How do we specify tokens?
  • Recognizing a token
  • How do we specify grammars?
  • Using grammars
  • Functional languages

4/7/2005 5

Specification of Programming

Languages

  • PLs require precise definitions (i.e. no

ambiguity)

  • Languageform (Syntax)
  • Languagemeaning (Semantics)
  • Consequently, PLs are specified using

formal notation:

  • Formal syntax
    • Tokens
    • Grammar
  • Formal semantics

4/7/2005 6

Lexical Structure of a PL

  • What do we mean by lexical structure?
    • the words or tokens of the program
  • What categories of tokens exist?
  • How do we identify tokens?

4/7/2005 7

Principle of Longest Substring

How many tokens should the following

be broken into, What are the

possibilities?

doif

x

456y

4/7/2005 8

Classic Fortran Example

DO 99 I = 1.

same as

DO99I = 1.10;

versus

DO 99 I = 1, 10

same as

for(I = 1; I <= 10; I++)

How many tokens?

4/7/2005 13

Recognizing Tokens

  • Tools: lex/flex/jlex
  • Hand coded scanners; FSM

4/7/2005 14

Scanner Example

  • Questions:
      1. What tokens are part of the language?
      1. Given the input: * + ( ) 42 # 345

What is the output?

4/7/2005 15

Syntax Analysis

  • Syntax:
    • Webster’s definition:1 a : the way in which linguistic elements (as words) are put together to form phrases
  • The syntax of a programming

language

  • Describes its form
    • i.e. Organization of tokens (elements)
  • Formal notation
    • Context Free Grammars (CFGs)

4/7/2005 16

Context Free Grammars

Notation

expression → identifier |number | - expression
| ( expression )
|expression operator expression
operator →^ +^ |^ -^ |^ *^ |^ /
  • Terminal symbols
  • Non-terminal symbols
  • Productions (i.e. grammar rule)

4/7/2005 17

Context Free Grammars

  • x+2*y
  • What tokens are recognized by the

following input stream?

  • id(x) + num(2) * id(y)

4/7/2005 18

CFG

E ::= E + T

| E - T

| T

T ::= T * F

|T / F

| F

F ::= num

| id

In general, a context-free grammar (CFG)
has a finite set of terminals (tokens), a finite
set of nonterminals from which one is the start
symbol, and a finite set of productions of the
form: A ::= X1 X2 ... Xn
where A is a nonterminal and each
Xi is either a terminal or nonterminal symbol.

4/7/2005 19

Derivations

  • A derivation shows how to generate a

syntactically valid string.

  • Example:
T / F + 1 - x => T * F / F + 1 - x
E ::= E + T
| E - T
| T
T ::= T * F
| T / F
| F
F ::= num
| id

4/7/2005 20

Derivation Example

  • Derivation of x * 2 + y => id(x) *

num(2) + id(y)

E ::= E + T
| E - T
| T
T ::= T * F
| T / F
| F
F ::= num
| id

4/7/2005 25

C (Figure 1.7, p. 26):

#include <stdio.h> int gcd(int u, int v) /* “functional” version / { if (v == 0) return u; else return gcd (v, u % v); / “tail” recursion / } main() / I/O driver */ { int x, y; printf("Input two integers:\n"); scanf("%d%d",&x,&y); printf("The gcd of %d and %d is %d\n", x,y,gcd(x,y)); return 0; } 4/7/2005 26

Java (Figure 1.9, p. 27):

import java.io.; class IntWithGcd { public IntWithGcd( int val ) { value = val; } public int getValue() { return value; } public int gcd ( int v ) { int z = value; / “imperative” version */ int y = v; while ( y != 0 ) { int t = y; y = z % y; z = t; } return z; } private int value; }

4/7/2005 27

Java (continued):

class GcdProg /* driver / { public static void main (String args[]) { System.out.println("Input two integers:"); BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); try / must handle I/O exceptions / { IntWithGcd x = / create an object */ new IntWithGcd(Integer.parseInt(in.readLine())); int y = Integer.parseInt(in.readLine()); System.out.print("The gcd of " + x.getValue()

  • " and " + y + " is "); System.out.println(x.gcd(y)); } catch ( Exception e) { System.out.println(e); System.exit(1); } } }

4/7/2005 28

Scheme (Figure 1.5, p. 17):

(define (gcd u v)

(if (= v 0) u

(gcd v (modulo u v))))

4/7/2005 29

Scheme (Figure 11.7, p. 486):

(define (euclid) ; sequential!
(display "enter two integers:")
(newline) ; goes to next line on screen
(let ((u (read)) (v (read)))
(display "the gcd of ")
(display u)
(display " and ")
(display v)
(display " is ")
(display (gcd u v))
(newline)))

4/7/2005 30

Paradigm use is rarely “pure”:

  • The C program defined the gcd

function in a purely functional style,

even though C is mainly imperative.

  • The Java program used some

imperative code to compute the gcd,

and was not completely object-

oriented (integers aren’t objects).

  • The Scheme code used sequencing to

do I/O, an imperative feature.

4/7/2005 31

Examples of languages that are

pure (mostly):

  • Imperative: (old) FORTRAN
  • Functional: Haskell
  • Object-oriented: Smalltalk

4/7/2005 32

Introduction to Scheme

  • Scheme started as an experiment in

programming language design.

  • It emerged from MIT in the mid-1970's.
  • The language was designed to have very

few, regular constructs which compose well

to support a variety of programming styles

including functional, object-oriented, and

imperative.

4/7/2005 37

• ((car (cdr (list + - * /))) 17 5)

• b.(cons (quote -) (cdr (quote (+ b c))))

c.(cdr (cdr '(a b c)))

• d.(cons 'd (cdr (cdr '(a b c d e f))))

e.(cons (+ '2 1/2) (list (- '3 1/3) (+ '

4/7/2005 38

• a. 1.2 × (2 - 1/3) + -8.

• b. (2/3 + 4/9)/(5/11 - 4/3)

• c. 1 + 1/(2 + 1/(1 + 1/2))

• d. 1 × -2 × 3 × -4 × 5 × -6 × 7

4/7/2005 39

Lists in SchemeLists in Scheme

z Lists are (essentially) the only data structure in
Scheme. However, lists are very versatile and can
be used to model any other data structure.
z Lists are constructed recursively using the empty
list '() and the cons binary operator:
(1 2 3) = (cons 1 (cons 2 (cons 3 '())))
z The first element of a list (its head) is returned by
the car operator: (car '(1 2 3)) = 1
z The tail of a list is returned by the cdr operator:
(cdr '(1 2 3)) = (2 3)

4/7/2005 40

Lists

• cons

– takes two arguments and returns a pair

(a list).

(cons '1 '(2 3 4)) Î (1 2 3 4)

(cons '(1 2 3) '(4 5 6)) Î ((1 2 3) 4 5 6)

(cons '1 '2) Î (1. 2)

4/7/2005 41

List Functions

  • car
    • returns the first member of a list or

dotted pair.

(car '(123 245 564 898)) Î 123

(car '(first second third)) Îfirst

(car '(this (is no) more difficult)) Î this

4/7/2005 42

More List Functions

  • cdr
    • returns the list without its first item, or

the second member of a dotted pair.

(cdr '(7 6 5)) Î (6 5)

(cdr '(it rains every day)) Î (rains every

day)

(cdr (cdr '(a b c d e f))) Î (c d e f)

(car (cdr '(a b c d e f))) Î b

4/7/2005 43

Even More List Functions

  • null?
    • returns #t if the object is the empty

list, (). It returns the null list, () (which

is the same as #f), if the object is

anything else.

  • length
    • returns the length of a list.

(length '(1 3 5 9 11)) Î 5

4/7/2005 44

More List Functions

  • list
    • returns a list constructed from its

arguments.

  • (list 'a) Î (a)
  • (list 'a 'b 'c 'd 'e 'f) Î (a b c d e f)
  • (list '(a b c)) Î ((a b c))
  • (list '(a b c) '(d e f) '(g h i)) Î

((a b c)(d e f)(g h i))

4/7/2005 49

Some Functional Language

Concepts

  • Referential Transparency
  • Metacircular Interpreter
  • High-order Functions
  • Garbage Collection
  • Delayed Evaluation
  • Lambda Calculus