Preparing for the ACW Languages & Compilers

Similar documents
PRACTICAL CLASS: Flex & Bison

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Module 8 - Lexical Analyzer Generator. 8.1 Need for a Tool. 8.2 Lexical Analyzer Generator Tool

Compiler Lab. Introduction to tools Lex and Yacc

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

CSE302: Compiler Design

Yacc: A Syntactic Analysers Generator

Introduction to Lex & Yacc. (flex & bison)

Lexical and Parser Tools

Chapter 3 Lexical Analysis

Syntax Analysis Part IV

Compiler course. Chapter 3 Lexical Analysis

Using Lex or Flex. Prof. James L. Frankel Harvard University

Lex & Yacc (GNU distribution - flex & bison) Jeonghwan Park

An Introduction to LEX and YACC. SYSC Programming Languages

(F)lex & Bison/Yacc. Language Tools for C/C++ CS 550 Programming Languages. Alexander Gutierrez

Lexical and Syntax Analysis

flex is not a bad tool to use for doing modest text transformations and for programs that collect statistics on input.

CSC 467 Lecture 3: Regular Expressions

Flex and lexical analysis

TDDD55- Compilers and Interpreters Lesson 2

CSCI Compiler Design

Flex and lexical analysis. October 25, 2016

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

COMPILER CONSTRUCTION Seminar 02 TDDB44

An introduction to Flex

Edited by Himanshu Mittal. Lexical Analysis Phase

Parsing and Pattern Recognition

Big Picture: Compilation Process. CSCI: 4500/6500 Programming Languages. Big Picture: Compilation Process. Big Picture: Compilation Process

CS143 Handout 04 Summer 2011 June 22, 2011 flex In A Nutshell

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

LECTURE 11. Semantic Analysis and Yacc

Parsing How parser works?

Big Picture: Compilation Process. CSCI: 4500/6500 Programming Languages. Big Picture: Compilation Process. Big Picture: Compilation Process.

Syntax-Directed Translation

Yacc Yet Another Compiler Compiler

COP 3402 Systems Software Syntax Analysis (Parser)

Introduction to Yacc. General Description Input file Output files Parsing conflicts Pseudovariables Examples. Principles of Compilers - 16/03/2006

CPS 506 Comparative Programming Languages. Syntax Specification

TDDD55 - Compilers and Interpreters Lesson 3

Chapter 2: Syntax Directed Translation and YACC

COMPILERS AND INTERPRETERS Lesson 4 TDDD16

CS4850 SummerII Lex Primer. Usage Paradigm of Lex. Lex is a tool for creating lexical analyzers. Lexical analyzers tokenize input streams.

CSE302: Compiler Design

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Principles of Programming Languages

Lex Spec Example. Int installid() {/* code to put id lexeme into string table*/}

Syntax. A. Bellaachia Page: 1

TDDD55- Compilers and Interpreters Lesson 3

Etienne Bernard eb/textes/minimanlexyacc-english.html

Lex & Yacc. By H. Altay Güvenir. A compiler or an interpreter performs its task in 3 stages:

Programming Language Syntax and Analysis

Automated Tools. The Compilation Task. Automated? Automated? Easier ways to create parsers. The final stages of compilation are language dependant

As we have seen, token attribute values are supplied via yylval, as in. More on Yacc s value stack

A program that performs lexical analysis may be termed a lexer, tokenizer, or scanner, though scanner is also a term for the first stage of a lexer.

Concepts. Lexical scanning Regular expressions DFAs and FSAs Lex. Lexical analysis in perspective

Implementation of Lexical Analysis

Chapter 4. Lexical and Syntax Analysis

Automatic Scanning and Parsing using LEX and YACC

Chapter 4. Lexical analysis. Concepts. Lexical scanning Regular expressions DFAs and FSAs Lex. Lexical analysis in perspective

Lex & Yacc. by H. Altay Güvenir. A compiler or an interpreter performs its task in 3 stages:

Using an LALR(1) Parser Generator

Project 2 Interpreter for Snail. 2 The Snail Programming Language

Compiler Design 1. Yacc/Bison. Goutam Biswas. Lect 8

Type 3 languages. Regular grammars Finite automata. Regular expressions. Deterministic Nondeterministic. a, a, ε, E 1.E 2, E 1 E 2, E 1*, (E 1 )

Introduction to Lexical Analysis

EXPERIMENT NO : M/C Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM,500GB HDD

Compil M1 : Front-End

Implementation of Lexical Analysis

Context-free grammars

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

Compiler Construction

Chapter 3. Describing Syntax and Semantics ISBN

Lexical and Syntax Analysis

Building Compilers with Phoenix

Ray Pereda Unicon Technical Report UTR-02. February 25, Abstract

Compiler Construction: Parsing

Compiler Construction

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Lexical Analysis. Lecture 3. January 10, 2018

Lexical Analysis. Implementing Scanners & LEX: A Lexical Analyzer Tool

CSE 3302 Programming Languages Lecture 2: Syntax

4. Lexical and Syntax Analysis

Applications of Context-Free Grammars (CFG)

Introduction to Lexical Analysis

Table of Contents. Chapter 1. Introducing Flex and Bison

Ulex: A Lexical Analyzer Generator for Unicon

Compiler Construction

Programming Languages (CS 550) Lecture 4 Summary Scanner and Parser Generators. Jeremy R. Johnson

Compiler Construction

LECTURE 6 Scanning Part 2

Part 5 Program Analysis Principles and Techniques

LECTURE 7. Lex and Intro to Parsing

4. Lexical and Syntax Analysis

Syntax. In Text: Chapter 3

Introduction to Lexing and Parsing

Handout 7, Lex (5/30/2001)

Gechstudentszone.wordpress.com

Implementation of Lexical Analysis

YOLOP Language Reference Manual

Transcription:

Preparing for the ACW 08348 Languages & Compilers

Introductory Lab There is an Introductory Lab Just involves copying the lab task See separate Lab slides

Language Roadmaps Convenient way of showing syntax in a diagram Used in ACW

Formal Specification of Languages Covered later in more detail Allows automation of language processing A language for describing languages A worthwhile investment of time Basic Structure α β α, β are strings; collection of symbols

Symbols α, β are strings containing Terminals Language component Non-terminals Names of rules Example Sentence -> Noun Verb Noun Noun -> Bill Noun -> Jane Verb -> likes Verb -> knows

BNF Backus Naur Form Expressions enclosed in angle brackets Vertical line indicates OR (choice) operation Used to express language syntax rules <digit> ::= 0 1 2 3 4 5 6 7 8 9 <integer> ::= <digit> <integer> <digit>

First ACW Task Convert Roadmaps to BNF Type this up, either in word or text file Needed for creating parser later Do not delay or spend too long Will delay coursework significantly Show to demonstrator to be signed off Do it at home before the lab Bring question/problems to lab Save completed BNF for documentation later

Compiler Structure

Regular Expressions Covered in Discrete Maths Finite State Machines Used to describe elements of a language Keywords : BEGIN, END, FOR Numbers : 1-2 3.74 2.6e5 Variables : count, a, Sum, Total Others: Comments/Strings

Writing Regular Expressions Define expressions using rules Symbol = RegExp Letter-A = a Letter-B = B Letter-C = C Begin = BEGIN

Writing Regular Expressions Sets of Letters digits = [0-9] letters = [a-za-z] Names of Regular Expressions cardinal = {number} Repetition Use * for zero or more Use + for 1 or more (note x+ == xx* )

Writing Regular Expression Use stick for choice Use parenthesis for grouping E.g: letters = [a-za-z] digits = [0-9] varname = {letters}({letters} {digits})*

Second ACW Task Create lex/flex lexical analyser With regular expression for language elements Test on sample programs Save output for documentation later Show to demonstrator Start outside the lab Bring question problems to the lab Look at initial lab task for pointers

Lexical Analysis lex is a unix tool to build lexical analysers flex is the same for linux/cygwin Apple Mac has unix tools Define lexemes for tokens Generates program in C from rules lex.yy.c C source for lexical analyser yylex() function gives tokens

Lex Program Declarations %% Translation rules %% Auxiliary procedures

Example Lex Program delim [ \t] ws {delim}+ digit [0-9] number {digit}+ %% int k; {ws} ; /* white space, skip */ \n printf( NEWLINE\n ); {number} {k = atoi(yytext); printf( unsigned integer: %d\n,k); } + printf( binaryop: PLUS\n ); * printf( binaryop: TIMES\n ); ( printf( bracket: BRA\n ); ) printf( bracket: KET\n );

Example Output Unix % lex calclex.l Unix % wc l lex.yy.c 330 lex.yy.c Unix % cc lex.yy.c ll Unix %./a.out 12 + 23 unsigned integer: 12 binaryop: PLUS unsigned integer: 23 NEWLINE (1+2)*3 bracket: BRA unsigned integer: 1 binaryop: PLUS unsigned integer: 2 bracket: KET binaryop: TIMES unsigned integer: 3 NEWLINE Unix % Cygwin % flex calclex.l Cygwin % gcc lex.yy.c o mylex.exe lfl Cygwin %./mylex ^D -- End of File

Lex syntax White Space has significance! E.g. word {letter} {digit} Means spaces required between letters and digits Translation Rules Expression Action Expression MUST be at start of line Action MUST NOT be at start of line Don t leave lines of whitespace

Third ACW Task Create parser rules from BNF Get yacc/bison to accept rules Grammar ambiguities need resolving Import tokens from lexical analyser Need to modify lexer to return tokens Test Parser rules using debug mode Use example test programs Save output for documentation Show to demonstrator

Building Parser Create Parser using yacc Called bison on linux/cygwin Available on Apple Mac Produces a parser in C Takes input from lexical analyser Language defined using grammar (based on BNF)

YACC Program Declarations %% Translation rules %% Auxiliary procedures

Calculator Example Consider Grammar Lines -> Line Line Lines Line -> Expr newline Expr -> Expr + Term Term Term -> Term * Factor Factor Factor -> ( Expr ) Number

Calculator YACC %token NUMBER PLUS TIMES BRA KET NEWLINE %% lines : line line lines ; line : expr NEWLINE { printf ( line: value: %d\n, $1); } ; expr : expr PLUS term {$$ = $1 + $3;} term {$$ = $1;} ; term : term TIMES factor {$$ = $1 * $3;} factor {$$ = $1;} ; factor : BRA expr KET {$$ = $2;} NUMBER {$$ = $1;} ; %% #include lex.yy.c

Calculator Lex Program delim [ \t] ws {delim}+ digit [0-9] number {digit}+ %% {ws} ; /* white space, skip */ \n return(newline); {number} {yylval = atoi(yytext); return(number); } + return(plus); * return(times); ( return(bra); ) return(ket);

Building the Calculator Unix % lex arithlex.l Unix % yacc arithparse.y Unix % cc y.tab.c ly ll Unix %./a.out 2 + (3 * 5) Line: Value: 17 (1+5) Line: value: 6 Unix % flex arithlex.l Unix % bison arithparse.y Unix % gcc arithparse.tab.c lfl o arith.exe Unix %./arith ^D Unix %./a.out < Prog.a > Proga-out.txt

Yacc Syntax Declaration part Imports token from lex Indicates start symbol C language declarations Translation Rules Of format P : Rule { action}