Program Development Tools. Lexical Analyzers. Lexical Analysis Terms. Attributes for Tokens

Similar documents
Concepts Introduced in Chapter 3. Lexical Analysis. Lexical Analysis Terms. Attributes for Tokens

Flex and lexical analysis

Flex and lexical analysis. October 25, 2016

Basic vi Commands. What is vi? To Get Into and Out Of vi

vi filename edit filename starting at line 1

VIP Quick Reference Card

An Introduction to LEX and YACC. SYSC Programming Languages

CSC 467 Lecture 3: Regular Expressions

Edited by Himanshu Mittal. Lexical Analysis Phase

Statistics 202A - vi Tutorial

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

Computer Programming Lecture 3 이윤진서울대학교

Using the Vi Text Editor

vi Primer Adapted from:

VI Commands Cheat Sheets

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

Emacs manual:

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

Editors in Unix come in two general flavours:

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

Lecture Outline. COMP-421 Compiler Design. What is Lex? Lex Specification. ! Lexical Analyzer Lex. ! Lex Examples. Presented by Dr Ioanna Dionysiou

Text Editors for Programmers. Dr. Prasad Kulkarni Michael Jantz Jamie Robinson

Text Editors for Programmers. EECS-678 Staff

CMPT 300. Operating Systems. Brief Intro to UNIX and C

Bok, Jong Soon

The structure of a compiler

VIM Tips and Tricks. Search and replace a string in a document : :.,$s/search_string/replacement_string/g

Figure 2.1: Role of Lexical Analyzer

Introduction to Lex & Yacc. (flex & bison)

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

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

Once you compile, Vim automatically takes you to the first line that is causing the error. To go to the next error; use :cn to take you to the next li

Introduction to Linux (Part II) BUPT/QMUL 2018/03/21

VIM Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : May, More documents are freely available at PythonDSP

FILE MAINTENANCE COMMANDS

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

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

Basic File Attributes

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

Linux 下代码查看 编辑 编译与调试 (I) 袁华

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

Handout 7, Lex (5/30/2001)

Lexical and Parser Tools

Software I: Utilities and Internals. What is vi?

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

BEG 6. 50p. Getting Started with the Emacs Screen Editor. An introduction to the Emacs screen editor, which is available on Unix systems.

Mastering Linux by Paul S. Wang Appendix: The emacs Editor

Lab 3a Using the vi editor

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

Learning the vi text editor

<<<<<<<<<<BASIC VI COMMANDS>>>>>>>>>>>>>>>>>>

LECTURE 6 Scanning Part 2

VI (and Clone) Introductory Guide

Marcello Bersani Ed. 22, via Golgi 42, 3 piano 3769

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

Using nu/tpu on UNIX Last revised: 1/28/00

Operating System Interaction via bash

PRINCIPLES OF COMPILER DESIGN UNIT II LEXICAL ANALYSIS 2.1 Lexical Analysis - The Role of the Lexical Analyzer

INTRODUCTION TO VIM. John Kerl University of Arizona Department of Mathematics Software Interest Group September 7, 2005

An introduction to Flex

L L G E N. Generator of syntax analyzier (parser)

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

Vim Habits. Detect inefficiency Find a quicker way Make it a habit

LEX/Flex Scanner Generator

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 )

The Language for Specifying Lexical Analyzer

Mills HPC Tutorial Series. Linux Basics I

Lexical Analysis - Flex

entity tomove over backward forward character C-b C-f word M-b M-f line C-p C-n go to line beginning (or end) C-a C-e sentence M-a M-e paragraph M- M-

PRACTICAL CLASS: Flex & Bison

Appendix C. Icon and Text Editors

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

Compiler Construction

The Xemacs Editor. Overview

Computer Networks Vim and Emacs. Seongjin Lee

FSASIM: A Simulator for Finite-State Automata

Week Overview. Unix file system File types and file naming Basic file system commands: pwd,cd,ls,mkdir,rmdir,mv,cp,rm man pages

Compiler Construction

Parsing and Pattern Recognition

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Languages and Compilers

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

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.

Contents Coding standard Debugging tool Text editor Version control system

Linux/Cygwin Practice Computer Architecture

Lexical Analysis. Sukree Sinthupinyo July Chulalongkorn University

Gechstudentszone.wordpress.com

Outline. Structure of a UNIX command

CS101 Linux Shell Handout

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

Compiler Construction Assignment 2 Fall 2009

CS 297 Report. By Yan Yao

Preparing for the ACW Languages & Compilers

7.1 INTRODUCTION 7.2 STARTING Emacs 7.3 Emacs SCREEN Menu Bar Text Mode Menu Mode Line Echo Line

A lexical analyzer generator for Standard ML. Version 1.6.0, October 1994

Essential Unix (and Linux) for the Oracle DBA. Revision no.: PPT/2K403/02

VIM David Orman July 14, 2009

LECTURE 7. Lex and Intro to Parsing

Intel Edison Tutorial: Introduction to Vim 1

Introduction to EMACS. Harriet Borton Academic and Research Computing

Transcription:

Program Development Tools lex makefiles vi and gvim ctags source level debugging diff and cmp Lexical Analyzers A lexical analyzer reads in a stream of characters as input and produces a sequence of symbols called tokens as output. Useful for a variety of tasks. Tools exist to automatically create a lexical analyzer from a specification. svn gprof Lexical Analysis Terms A token is a group of characters having a collective meaning (e.g. id). A lexeme is an actual character sequence forming a specific instance of a token (e.g. num). A pattern is the rule describing how a particular token can be formed (e.g. [A-Za-z_][A-Za-z_0-9]*). Characters between tokens are called whitespace (e.g.blanks, tabs, newlines, comments). Attributes for Tokens Tokens can have attributes that can be passed back to the function calling the lexical analyzer. Constants value of the constant Identifiers pointer to a location where information is kept about the identifier

General Approaches to Implementing Lexical Analyzers Use a lexical-analyzer generator, such as Lex. Lex - A Lexical Analyzer Generator Can link with a lex library to get a main routine. Can use as a function called yylex(). Easy to interface with yacc. Write the lexical analyzer in a conventional programming language. Write the lexical analyzer in assembly language. Lex Source { definitions } %% { rules } %% { user subroutines } Lex Specifications Definitions declarations of variables, constants, and regular definitions Rules regular expression Regular Expressions action operators ''\ [ ] ^ -?. * + ( ) $ / { } actions C code Lex Regular Expression Operators s string s literally \c character c literally (used when c would normally be used as a lex operator) [s] for defining s as a character class ^ to indicate the beginning of a line [^s] means to match characters not in the s character class [a-b] used for defining a range of characters (a to b) in a character class r? means that r is optional

Lex Regular Expression Operators (cont.). means any character but a newline r* means zero or more occurances of r r+ means one or more occurances of r r1 r2 r1 or r2 (r) r (used for grouping) $ means the end of the line r1/r2 means r1 when followed by r2 Example Regular Expressions in Lex a* zero or more a's a+ one or more a's [abc] a, b, or c [a-z] lower case letter [a-za-z] any letter [^a-za-z] any character that is not a letter a.b a followed by any character followed by b ab cd ab or cd a(b c)d abd or acd ^B B at the beginning of line E$ E at the end of line r{m,n} means m to n occurences of r Lex Specifications (cont.) Actions Actions are C source fragments. If it is compound or takes more than one line, then it should be enclosed in braces. Example Rules [a-z]+ [A-Z][a-z]* Definitions name printf(''found word\n''); { printf(''found capitalized word\n''); printf{'' %s\n'', yytext); } translation Example Definition digits [0-9] Example Lex Program digits [0-9] ltr [a-za-z] alpha [a-za-z0-9] %% [ +]{digits}+ {digits}+ printf(''number: %s\n'', yytext); {ltr}(_ {alpha})* printf(''identifier: %s\n'', yytext); "'"."'" printf(''character: %s\n'', yytext);. printf(''?: %s\n'', yytext); Prefers longest match and earlier of equals.

Another Example Lex Program %% BEGIN { return (1); } END { return (2); } IF { return (3); } THEN { return (4); } ELSE { return (5); } letter(letter digit)* { return (6); } digit+ { return (7); } < { return (8); } <= { return (9); } = { return (10); } <> { return (11); } > { return (12); } >= { return (13); } Make and Makefiles The make utility is used to: Automate the execution of commands for file generation. Minimize the number of commands needed for rebuilding a target. A makefile describes a hierarchy of dependencies between individual files commands to generate each file Make and Makefiles (cont.) There can be one or more target entries in a makefile. Each target entry in a makefile has the following format: target : dependency_file... command... The target is a file. There can be one or more dependency files on which the target depends. There can be one or more commands each preceded by a tab that comprise a rule for the target. These commands are used to create or regenerate the target file. Make and Makefiles (cont.) A target is remade when the target file either does not exist or has an older date/time than one or more of the dependency files. The targets and dependency files comprise a DAG structure representing the dependencies between the different components. The make utility recursively checks each target against its dependencies, starting with the first target entry in the makefile.

Example Makefile Invoking Make expr.exe : expr.o lex.yy.o gcc -g -o expr.exe expr.o lex.yy.o expr.o : expr.c defs.h gcc -g -c expr.c lex.yy.o : lex.yy.c gcc -g -c lex.yy.o lex.yy.c : scan.l lex scan.l General form. make [ -f makefilename ] [ target ] If no makefilename is given, then make looks for a file called makefile or Makefile in that order. Make uses one of these files if found in the current directory. By default make attempts to create the first target in the file. Alternatively, a user can specify a specific target within the makefile to make. Example Invocations of Make Defining Symbols in a Makefile # Make the first target in makefile or Makefile. make # Make the lex.yy.o target in makefile or Makefile. make lex.yy.o # Make the first target in the makeexpr makefile. make -f makeexpr # Make the expr.o target in the makeexpr makefile. make -f makeexpr expr.o You can define a symbol in a makefile and reference the symbol in multiple places. General form of the definition. symbol = definition General form of the reference. $(symbol)

Example Makefile with Symbols CC = gcc CFLAGS = -g -c expr.exe : expr.o lex.yy.o $(CC) -g -o expr.exe expr.o lex.yy.o expr.o : expr.c defs.h $(CC) $(CFLAGS) expr.c lex.yy.o : lex.yy.c $(CC) $(CFLAGS) lex.yy.o The Vi Editor (17.1) Vi stands for the VIsual editor. Why use the vi editor? The vi editor is standard on every Unix system. The vi editor allows you to use ex line commands. The vi editor has many special features that are very useful. The vi editor is efficient compared to emacs. lex.yy.c : scan.l lex scan.l Invoking Vi The vi editor is invoked by issuing the command in the following form. The -r option is for recovering a file where the system crashed during a previous editing session. The -t option is to indicate the position within a file the editing should start. The use of tags will be discussed more later. vi [-t tag] [-r ] filename Vi Modes The vi editor has three main modes: character input mode: where text can be entered insert, append, replace, add lines window mode: where regular commands can be issued basic cursor motions screen control word commands deletions control commands miscellaneous commands line mode: where ex commands can be issued

Vi Character Input Mode After invoking vi, the user is in the window command mode. There are a few different commands to enter character input mode. At that point a user types in the desired text. A user selects the ESC key to return back to command mode. Vi Commands to Go into Character Input Mode The following commands are used to go into character input mode. All but the r command require the user to hit the ESC key to go back into window command mode. a A i I o O rchr R append text after the cursor position append text at the end of line insert text before the cursor position insert text before the first nonblank character in the line add text after the current line add text before the current line replace the current character with chr replace text starting at the cursor position Vi Basic Cursor Motions The basic cursor motions allow you to move around in the file. The letter options allow movement when the arrow keys are not defined. h go back one character j go down one line k go up one line l go forward one character (space also works) + CR go down one line to first nonblank character go up one line to first nonblank character 0 go to the beginning of the line $ go to the end of the line H go to the top line on the screen L go to the last line on the screen Vi Word Movements The following commands can be used to position the cursor. w b e position the cursor at the beginning of the next word position the cursor at the beginning of the previous word position the cursor at the end of the current word

Vi Screen Control The following commands can be used to control the screen being displayed. ^U ^D ^B ^F ^L scroll up one half page scroll down one half page scroll up one page scroll down one page redisplay the page Deletions in Vi The following vi commands can be used to delete text. dd D x X dw delete the current line delete text from the cursor to the end of the line delete character at the cursor delete character preceding the cursor delete characters from the cursor to the end of the word Searching in Vi The following vi commands can be used to search for text patterns, where each pattern is a regular expression. /pattern search forward for the specified pattern / search forward for the last specified pattern?pattern search backward for the specified pattern? search backward for the last specified pattern n perform the last / or? command Miscellaneous Vi Commands Below are some miscellaneous vi commands. Commands that delete text also place the deleted text into a buffer. u undo previous command U restore entire line Y save current line into buffer p put saved buffer after cursor position P put saved buffer before cursor position J join current line with following line % position cursor over matching (, ), {, or } ZZ save file and exit (same as :wq)

Repeating Vi Commands You can indicate how many times a particular command is to be performed. Examples: 3dd 4w 7x 5n delete 3 lines advance 4 words delete 7 characters perform last search 5 times Vi Line Command Mode You can enter line command mode by typing a colon. At that point you can enter ex commands. Examples: :150 # go to line 150 :+50 # go forward 50 lines :1,.d # delete lines from line 1 to current line :1,$ s/old/new/g # perform substitutions on the entire file :w [filename] # write file [to specified filename] :q # quit editing session :wq # write file and quit :q! # quit and don't save changes Ctags: Create a Tags File for Use with Vi ctags is a Unix utility that takes in a set of source files as input and creates a tags file as output. The tags file contains for each function and macro: Object name. File in which the object is defined. Pattern describing the location of the object. General form: ctags list_of_files Using the Tags File You can use the tags file with the vi (vim, gvim) editors. Use the -t option when invoking vi. The editor will look in the current directory for the tags file and attempt to find the location of the function or macro in the appropriate file. If it finds it, then the file is opened and the cursor is positioned to that location. vi -t tag Examples: vi -t main vi -t max

Vim: Vi IMproved, a Programmer's Text Editor Gvim: GUI Vim Below is a snapshot of using gvim. Vim is a text editor that is upwards compatible with vi. Enhancements over vi: multi-level undo syntax highlighting on-line help visual selection tag searching support Multi-Level Undo in Vim Can use the u command to undo multiple changes, as opposed to vi, which can only undo the last change. Each time you enter u, the previous change is undone. Can redo the last undone change by using the CTRL-R command. Each time you enter CTRL- R, the last undone change is reperformed. Syntax Highlighting in Vim Syntax highlighting allows vim to show parts of the text in another font or color. The rules for syntax highlighting are stored in a file. So vim can always be easily extended to support syntax highlighting in new types of files. Vim currently supports syntax highlighting for files with the following extensions: *.c, *.cpp, *.pl, *.sh, *.csh, *.java, *.html, *.tex, *.tr

Visual Selection in Vim Select text portions using the left mouse button. Some of the commands you can perform on visually selected portions include: d y U u gq delete the highlighted text place in a buffer make uppercase make lowercase format lines to width of window Can change the position of the cursor by clicking on a specific location in the window. Can use the middle mouse button to insert text selected by the left mouse button. Tag Searching Support in Vim Can position the cursor over a name, such as the name of a function. Typing CTRL-] will cause vim to search in the tags file for the name under the cursor. If found, then the file containing the function or macro name is opened and the cursor is positioned to that function. This command is equivalent to exiting vim and typing vi -t name On-line Help in Vim You can type :help to get online help in Vim. There are several keywords or files that can be selected within the on-line help to learn about specific vim features. The.gvimrc File The.gvimrc file is used to initialize various settings. Example commands often used in a.gvimrc file: :set incsearch :win width height :syntax on :set background=dark # shows text where the pattern has # matched so far when typing a search # command # set the width and the height in # characters of the window # enable syntax highlighting # set background of window to be # dark (default text will be white)