Programming Language Concepts CIS 280 - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Programming Language Concepts CIS 280

Description:

Compile time: data layout, internal data structures ... Regular expressions, regular grammars reasonable way to generates strings in language ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 24
Provided by: me6105
Category:

less

Transcript and Presenter's Notes

Title: Programming Language Concepts CIS 280


1
Programming Language Concepts (CIS 280)
  • Elsa L Gunter
  • NJIT
  • Fall 2001

2
Bindings of Program Elements
  • The binding time is when that selection is made

3
Binding Times
  • Language definition time language syntax and
    semantics
  • Language implementation time interpreter versus
    compiler, aspects left flexible in definition,
    set of available libraries

4
Binding Times
  • A binding of a program element is the selection
    of a particular value for it from a set of
    possible values
  • Compile time data layout, internal data
    structures
  • Link time (load time) binding of values to
    identifiers across program modules
  • Run time actual values assigned to non-constant
    identifiers

5
To class
  • In expression x y 5 what bindings can you
    identify, and when are they most likely made?

6
Program Aspects
  • Syntax what valid programs look like
  • Semantics what valid programs mean what they
    should compute
  • Compiler must contain both information

7
Major Phases of a Compiler
  • Lex
  • Break the source into separate tokens
  • Parse
  • Analyze phrase structure and apply semantic
    actions, usually to build an abstract syntax tree
  • Semantic analysis
  • Determine what each phrase means, connect
    variable name to definition (typically with
    symbol tables), check types

8
Major Phases of a Compiler
  • Translate to intermediate representation
  • Instruction selection
  • Optimize
  • Emit final machine code

9
Major Phases of a Compiler
Source Program
Lex
Relocatable Object Code
Instruction Selection
Tokens
Linker
Parse
Unoptimized Machine-Specific Assembly Language
Abstract Syntax
Machine Code
Semantic Analysis
Optimize
Optimized Machine-Specific Assembly Language
Symbol Table
Translate
Emit code
Intermediate Representation
Assembly Langague
Assembler
Modified from Modern Compiler Implementation in
ML, by Andrew Appel
10
Example of Optimization
  • Program code X Y Z W
  • Load reg1 with Y
  • Load reg2 with Z
  • Add reg1 and reg2, saving to reg1
  • Store reg1 to tmp
  • Load reg1 with tmp
  • Load reg2 with W
  • Add reg1 and reg2, saving to reg1
  • Store reg1 to X
  • Eliminate two steps marked

11
Language Syntax
  • Syntax is the description of which strings of
    symbols are meaningful expressions in a language
  • It takes more than syntax to understand a
    language need meaning (semantics) too
  • Syntax is the entry point

12
Features of a Good Syntax
  • Readable
  • Writeable
  • Lack of ambiguity
  • Suggestive of correct meaning
  • Ease of translation

13
Elements of Syntax
  • Character set previously typically ASCII, now
    often 64 character sets (UNICODE)
  • Keywords usually reserved
  • Special constants cannot be assigned to
  • Identifiers can be assigned to
  • Operator symbols
  • Delimiters (parenthesis, braces,brackets,)
  • Blanks (aka white space)

14
Elements of Syntax
  • Expressions
  • Type expressions
  • Declarations
  • Statements (in imperative languages)
  • Subprograms (subroutines)

15
Elements of Syntax
  • Modules
  • Interfaces
  • Classes (for object-oriented languages)
  • Libraries

16
Formal Language Descriptions
  • Regular expressions, regular grammars
  • Context-free grammars, BNF grammars, syntax
    diagrams
  • Finite state automata
  • Whole family more of grammars and automata
    covered in automata theory

17
Grammars
  • Grammars are formal descriptions of which strings
    over a given character set are in a particular
    language
  • Language designers write grammar
  • Language implementers use grammar to know what
    programs to accept
  • Language users use grammar to know how to write
    legitimate programs

18
Regular Expressions
  • Start with a given character set a,
    b, c
  • Build bigger regular expressions from smaller
    ones
  • Each character is a regular expression
  • It represents the set of one string containing
    just that character

19
Regular Expressions
  • If x and y are regular expressions, then xy is a
    regular expression
  • It represents the set of all strings made from
    first a string in x then a string in y
  • If x and y are regular expressions, then x?y is a
    regular expression
  • It represents the set of strings in either x or y

20
Regular Expressions
  • If x is a regular expression, then so is (x)
  • It represents the same thing as x
  • If x is a regular expression, then so is x
  • It represents strings made from concatenating
    zero or more strings from x
  • ?
  • It represents the empty set

21
Example Regular Expressions
  • (0?1)1
  • The set of all strings of 0s and 1s ending in
    1, 1, 01, 11,
  • ab(a)
  • The set of all strings of as and bs with
    exactly one b
  • ((01) ?(10))
  • You tell me
  • Regular expressions (equivalently, regular
    grammars) important for lexing, breaking strings
    into recognized words

22
Example Lexing
  • Regular expressions good for describing lexemes
    (words) in a programming language
  • Identifier (a ? b ? ? z ? A ? B ? ? Z) (a ?
    b ? ? z ? A ? B ? ? Z ? 0 ? 1 ? ? 9 ? _ ?
    )
  • Digit (0 ? 1 ? ? 9)
  • Number (1 ? ? 9)(0 ? ? 9) ? (1 ? ? 9)(0
    ? ? 9)
  • Keywords if if, while while,

23
Implementing Regular Expressions
  • Regular expressions, regular grammars reasonable
    way to generates strings in language
  • Not so good for recognizing when a string is in
    language
  • Regular expressions which option to choose, how
    many repetitions to make
  • Answer finite state automata
Write a Comment
User Comments (0)
About PowerShow.com