CSE 425: Syntax I Syntax and Semantics Syntax gives the structure of statements in a language –Allowed ordering, nesting, repetition, omission of symbols.

Slides:



Advertisements
Similar presentations
COS 320 Compilers David Walker. Outline Last Week –Introduction to ML Today: –Lexical Analysis –Reading: Chapter 2 of Appel.
Advertisements

4b Lexical analysis Finite Automata
CPSC Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of states Σ: finite set of “letters” (input alphabet)
Lexical Analysis Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!
COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou.
From Cooper & Torczon1 The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source language?
CSE 425: Semantic Analysis Semantic Analysis Allows rigorous specification of a program’s meaning –Lets (parts of) programming languages be proven correct.
CSE 425: Semantics II Implementing Scopes A symbol table is in essence a dictionary –I.e., every name appears in it, with the info known about it –Usually.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax Lecture 2 - Syntax, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai.
Lexical Analysis III Recognizing Tokens Lecture 4 CS 4318/5331 Apan Qasem Texas State University Spring 2015.
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
Chapter3: Language Translation issues
Compiler Construction
1 Chapter 3 Scanning – Theory and Practice. 2 Overview Formal notations for specifying the precise structure of tokens are necessary  Quoted string in.
Chapter 3: Formal Translation Models
Scanner Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source language? Is the.
1 Scanning Aaron Bloomfield CS 415 Fall Parsing & Scanning In real compilers the recognizer is split into two phases –Scanner: translate input.
EECS 6083 Intro to Parsing Context Free Grammars
CPSC 388 – Compiler Design and Construction
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Compiler1 Chapter V: Compiler Overview: r To study the design and operation of compiler for high-level programming languages. r Contents m Basic compiler.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
1 Chapter 3 Scanning – Theory and Practice. 2 Overview Formal notations for specifying the precise structure of tokens are necessary –Quoted string in.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
By: Er. Sukhwinder kaur.  What is Automata Theory? What is Automata Theory?  Alphabet and Strings Alphabet and Strings  Empty String Empty String 
Lecture # 3 Chapter #3: Lexical Analysis. Role of Lexical Analyzer It is the first phase of compiler Its main task is to read the input characters and.
Grammars CPSC 5135.
Lexical Analyzer (Checker)
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
CS412/413 Introduction to Compilers Radu Rugina Lecture 4: Lexical Analyzers 28 Jan 02.
COMP3190: Principle of Programming Languages DFA and its equivalent, scanner.
CS 461 – Sept. 19 Last word on finite automata… –Scanning tokens in a compiler –How do we implement a “state” ? Chapter 2 introduces the 2 nd model of.
CSE 425: Syntax II Context Free Grammars and BNF In context free grammars (CFGs), structures are independent of the other structures surrounding them Backus-Naur.
1 Languages and Compilers (SProg og Oversættere) Lexical analysis.
Lexical Analysis: Finite Automata CS 471 September 5, 2007.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
CPS 506 Comparative Programming Languages Syntax Specification.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Overview of Previous Lesson(s) Over View  Symbol tables are data structures that are used by compilers to hold information about source-program constructs.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
CSE 425: Control Abstraction II Exception Handling Previous discussion focuses on normal control flow –Sometimes program reaches a point where it cannot.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
Chapter 2 Scanning. Dr.Manal AbdulazizCS463 Ch22 The Scanning Process Lexical analysis or scanning has the task of reading the source program as a file.
using Deterministic Finite Automata & Nondeterministic Finite Automata
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 3.
LECTURE 5 Scanning. SYNTAX ANALYSIS We know from our previous lectures that the process of verifying the syntax of the program is performed in two stages:
1 Compiler Construction Vana Doufexi office CS dept.
Spring 16 CSCI 4430, A Milanova 1 Announcements HW1 will be out this evening Due Monday, 2/8 Submit in HW Server AND at start of class on 2/8 A review.
Deterministic Finite Automata Nondeterministic Finite Automata.
COMP3190: Principle of Programming Languages DFA and its equivalent, scanner.
Lecture 2 Compiler Design Lexical Analysis By lecturer Noor Dhia
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Names and Attributes Names are a key programming language feature
CS510 Compiler Lecture 2.
Lecture 2 Lexical Analysis
CS 326 Programming Languages, Concepts and Implementation
Chapter 2 Scanning – Part 1 June 10, 2018 Prof. Abdelaziz Khamis.
Lexical Analysis (Sections )
CSE 3302 Programming Languages
CS 3304 Comparative Languages
Iteration Implemented through loop constructs (e.g., in C++)
CS 3304 Comparative Languages
High-Level Programming Language
Compiler Construction
Presentation transcript:

CSE 425: Syntax I Syntax and Semantics Syntax gives the structure of statements in a language –Allowed ordering, nesting, repetition, omission of symbols –Can automate the process of checking correct syntax Semantics give meaning to the (structured) symbols –E.g., kinds of labels, types of variables, layout of classes –For example, what does 11 mean? (at least 3 good answers) Separating syntactic and semantic evaluation helps –Can isolate the problem of syntactic recognition in an engine –Can use the structure produced by the engine directly –Sometimes called syntax-directed (compiler in charge)

CSE 425: Syntax I Syntax and Lexical Structure Syntax gives the structure of statements in a language –E.g., the format of tokens and how they can be arranged –Lexical structure also describes how to recognize them Scanning obtains tokens from a stream characters –E.g., whitespace delimited vs. regular-expression based –Tokens include keywords, constants, symbols, identifiers –Usually based on assumption of taking longest substring Parsing recognizes more complex expressions –E.g., well-formed statements in logic, arithmetic, etc. –Free-format languages ignore indentation, etc. while fixed format languages have specific restrictions/requirements

CSE 425: Syntax I Scanning vs. Parsing Roles It is often possible to simplify a grammar’s structure by making its tokens more sophisticated –For example, scanning for the terminal token NUMBER vs. parsing for the non-terminal number → nonzerodigit digit* Such simplification delegates work to a scanner –Often this is a good separation of concerns, especially since scanning may appropriately specialize it logic, etc. –E.g., a fairly general scanner built from classification functions (which look for all digits, all alphabetic, etc.) can be re-used or refactored easily for scanning different grammars –E.g., the C++11 library is worth studying and using

CSE 425: Syntax I Regular Expressions, DFAs, NDFAs Regular expressions capture lexical structure of symbols that can be built using 3 composition rules –Concatenation (ab), selection (a | b), repetition (b*) Finite automata can recognize regular expressions –Deterministic finite automata (DFAs) associate a unique state with each sequence generated by a regular expression –Non-deterministic finite automata (NDFAs) let multiple states to be reached by the same input sequence (adding “choice”) Can generate a unique (minimal) DFA in 3 steps –Generate NDFA from the regular expression (Scott pp. 56) –Convert NDFA to (possibly larger) DFA (Scott pp ) –Minimize the DFA (Scott pp. 59) to get a unique automaton C++11 library automates all this for you

CSE 425: Syntax I Today’s Studio Exercises We’ll code up some ideas from Scott Chapter –Looking at mechanisms for recognizing tokens and for parsing basic CFGs with straightforward recursion –Next studio we’ll look at more complicated variations Today’s exercises are all in C++ –We’ll write our own code, but check out the library too, since you’ll be allowed to use it for lab assignments! –Please take advantage of the on-line tutorial and reference manual pages that are linked on the course web site –As always, please ask us for help as needed When done, your answers to the course account with “Syntax Studio I” in the subject line