Compiler Techniques MN1 The nano-c Language

Similar documents
The SPL Programming Language Reference Manual

Decaf Language Reference

YOLOP Language Reference Manual

1 Lexical Considerations

The PCAT Programming Language Reference Manual

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Lexical Considerations

A Simple Syntax-Directed Translator

Lexical Considerations

Typescript on LLVM Language Reference Manual

GAWK Language Reference Manual

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

Syntax. A. Bellaachia Page: 1

cmps104a 2002q4 Assignment 3 LALR(1) Parser page 1

CPS 506 Comparative Programming Languages. Syntax Specification

University of Utrecht. 1992; Fokker, 1995), the use of monads to structure functional programs (Wadler,

Project 2 Interpreter for Snail. 2 The Snail Programming Language

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

More Assigned Reading and Exercises on Syntax (for Exam 2)

DEMO A Language for Practice Implementation Comp 506, Spring 2018

IC Language Specification

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Crayon (.cry) Language Reference Manual. Naman Agrawal (na2603) Vaidehi Dalmia (vd2302) Ganesh Ravichandran (gr2483) David Smart (ds3361)

Language Reference Manual

KU Compilerbau - Programming Assignment

Decaf Language Reference Manual

Principles of Programming Languages COMP251: Syntax and Grammars

A Short Summary of Javali

Part 5 Program Analysis Principles and Techniques

Funk Programming Language Reference Manual

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Full file at C How to Program, 6/e Multiple Choice Test Bank

A simple syntax-directed

SMPL - A Simplified Modeling Language for Mathematical Programming

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

2.2 Syntax Definition

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

JME Language Reference Manual

Annex A (Informative) Collected syntax The nonterminal symbols pointer-type, program, signed-number, simple-type, special-symbol, and structured-type

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

M/s. Managing distributed workloads. Language Reference Manual. Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567)

C Language, Token, Keywords, Constant, variable

ASML Language Reference Manual

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

DINO. Language Reference Manual. Author: Manu Jain

Reference Grammar Meta-notation: hfooi means foo is a nonterminal. foo (in bold font) means that foo is a terminal i.e., a token or a part of a token.

Chapter 2. Lexical Elements & Operators

CS109A ML Notes for the Week of 1/16/96. Using ML. ML can be used as an interactive language. We. shall use a version running under UNIX, called

Learning Language. Reference Manual. George Liao (gkl2104) Joseanibal Colon Ramos (jc2373) Stephen Robinson (sar2120) Huabiao Xu(hx2104)

Examination in Compilers, EDAN65

1. Lexical Analysis Phase

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Data Types and Variables in C language

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

PLT 4115 LRM: JaTesté

Programming in C++ 4. The lexical basis of C++

CS:3820 Programming Language Concepts

Differentiate Between Keywords and Identifiers

Advanced Algorithms and Computational Models (module A)

Introduction to Lexical Analysis

The Warhol Language Reference Manual

4. Inputting data or messages to a function is called passing data to the function.

An Interactive Desk Calculator. Project P2 of. Common Lisp: An Interactive Approach. Stuart C. Shapiro. Department of Computer Science

Chapter 3. Describing Syntax and Semantics ISBN

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Sprite an animation manipulation language Language Reference Manual

Decaf Language Reference

Learning to program is a lot like learning to speak a new language. You must learn new vocabulary,

TML Language Reference Manual

UNIT- 3 Introduction to C++

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals

Lexical Analysis (ASU Ch 3, Fig 3.1)

CiviC Language Manual

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Programs as Data 6 Imperative languages, environment and store, micro-c

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

MR Language Reference Manual. Siyang Dai (sd2694) Jinxiong Tan (jt2649) Zhi Zhang (zz2219) Zeyang Yu (zy2156) Shuai Yuan (sy2420)

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

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

Implementing a VSOP Compiler. March 12, 2018

Basics of Java Programming

Template Language and Syntax Reference

Reference C0.2. bool The type of booleans has just two elements, true and false. They are used for conditions, as well as contracts.

Introduction to C# Applications

Chapter 3 Lexical Analysis

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

DECISION MAKING STATEMENTS

Syntax. In Text: Chapter 3

INTRODUCTION 1 AND REVIEW

ITEC2620 Introduction to Data Structures

Spoke. Language Reference Manual* CS4118 PROGRAMMING LANGUAGES AND TRANSLATORS. William Yang Wang, Chia-che Tsai, Zhou Yu, Xin Chen 2010/11/03

Compiler course. Chapter 3 Lexical Analysis

Lecture 3. More About C

3. Semantic analysis generates intermediate code from the AST, also performing type checking. This will be the topic of Lab 3.

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Introduction to C Programming

This book is licensed under a Creative Commons Attribution 3.0 License

The Lexical Structure of Verdi TR Mark Saaltink. Release date: July 1994

Review of the C Programming Language

Transcription:

Compiler Techniques MN1 The nano-c Language February 8, 2005 1 Overview nano-c is a small subset of C, corresponding to a typical imperative, procedural language. The following sections describe in more detail which language elements are present. Every nano-c program is also a valid C program. The syntax and semantics of nano-c is the same as that for full C, within the restrictions decribed here. 2 Lexical elements Decimal integer constants. Alphanumeric identiers: non-empty sequences of letters or digits starting with a letter. Keywords: else, if, int, return, void, and while. Special symbols:!=,!, &&, (, ), *, +,, (comma), -, /, ;, <=, <, ==, =, >=, >, [, ],,,. White space characters: blank (32), newline (10), carriage return (13), form feed (12), and tab (9). Comments: /* followed by anything and terminated by */, and // followed by anything and terminated by end of line. 3 Syntax Primary expressions: constants, identiers, function calls, array indexing, and parentheses. Unary expressions with the! and - unary operators. Binary expressions with the +, -, *, /, <, >, <=, >=, ==,!=, &&, and = operators. Statements: expression statements, the empty statement, if statements with or without else, while statements, return statements, and compound statements (blocks) statements. 1

Local variable declarations are only permitted at the top-level function body block, not in nested blocks. Variable declarations: base type followed by variable name, and for arrays followed by the array size in square brackets. The only allowed base type is int. Multi-dimensional arrays, pointers, and structures are not included. Arrays cannot be parameters to functions. Initialisers in variable declarations are not included. Function denitions: return type or void, function name, parameter list, and body (compound statement) in that order. The parameter list is either void, meaning no parameters, or a sequence of variable declarations separated by, (comma). An external (library) function can be declared by writing a function denition without body, terminated with a ; (semi-colon). Variable-arity functions are not included. 4 Program execution Execution starts at the user-dened function main which takes no parameters and returns int. Execution ends when main returns. The standard library is nano-c-specic since nano-c excludes variablearity functions, and this makes printf and scanf-like functions impossible. To use the library, include the following declarations at the start of your nano-c source le: void printchar(int ch); // prints to stdout void printint(int i); // prints to stdout int readchar(void); // reads from stdin int readint(void); // reads from stdin 2

5 Example /* This is an example nano-c program. */ void printint(int i); int fac(int n) if (n < 2) return n; return n * fac(n-1); int a[2]; int sum_a(int n) int i; int s; i = 0; s = 0; while (i < n) s = s + a[i]; i = i + 1; return s; int main(void) a[0] = fac(5); a[1] = 27; printint(sum_a(2)); // prints 147 return 0; 3

6 Informal grammar for nano-c This is an informal context-free grammar for nano-c: The start symbol is program. Terminals are written within double-quotes. /empty/ denotes the empty string. intconst and ident denote classes of lexical elements. Associativity and precedence for expression operators is not expressed. The grammar has not been adjusted to t any particular parsing method. program ::= topdec_list topdec_list ::= /empty/ topdec topdec_list topdec ::= vardec ";" fundec vardec ::= scalardec arraydec scalardec ::= typename ident arraydec ::= typename ident "[" intconst "]" typename ::= "int" fundec ::= funtype ident "(" formals ")" funbody funtype ::= typename "void" funbody ::= "" locals stmts "" ";" formals ::= "void" formal_list formal_list ::= scalardec scalardec "," formal_list locals ::= /empty/ vardec ";" locals stmts ::= /empty/ stmt stmts stmt ::= expr ";" "return" expr ";" "return" ";" "while" condition stmt "if" condition stmt else_part "" stmts "" ";" else_part ::= /empty/ "else" stmt condition ::= "(" expr ")" expr ::= intconst ident ident "[" expr "]" unop expr expr binop expr ident "(" actuals ")" "(" expr ")" unop ::= "-" "!" binop ::= "+" "-" "*" "/" "<" ">" "<=" ">=" "!=" "==" " " "&&" "=" actuals ::= /empty/ expr_list expr_list ::= expr expr "," expr_list 4

6.1 Parsning top-level declarations Top-level declarations have parsing problems with both top-down and bottomup parsing methods. The problem comes from the fact that when int has been parsed and is followed by ident, the parser cannot determine if the int should be a typename for a variable declaration or a funtype for a function declaration. The rst half of the solution is to eliminate fundec and move its production into topdec. The second half of the solution can be done in two dierent ways. One alternative is to make two versions of the (old) fundec production, one with typename rst, and one with void rst. This eliminates the ambiguity, but the cost is an additional production. Another alternative is to eliminate funtype and allow void as an alternative in typename. This however means that the syntax tree can contain variable or array declarations with void as base type: they must be detected and rejected, either by the syntax-tree building code, or by the type checking pass. 6.2 Expression operator precedence table primary and postx expressions 16 ident intconst 16 a[i ] 16 f (...) prex unary operators 14 -! inx operators 13L * / 12L + - 10L < > <= >= 9L ==!= 5L && 4L 2R = The numbers to the left indicate precedence; larger numbers indicate higher precedence. L indicates left-associative operators and R indicates right-associative operators. The table only describes C operators included in nano-c. 5