A Grammar for the C- Programming Language (Version F16) September 20, 2016

Similar documents
Lexical Considerations

1 Lexical Considerations

Lexical Considerations

Mirage. Language Reference Manual. Image drawn using Mirage 1.1. Columbia University COMS W4115 Programming Languages and Translators Fall 2006

Decaf Language Reference Manual

COMS W4115 Programming Languages & Translators FALL2008. Reference Manual. VideO Processing Language (VOPL) Oct.19 th, 2008

The Decaf Language. 1 Lexical considerations

KU Compilerbau - Programming Assignment

The SPL Programming Language Reference Manual

Introduction to C# Applications

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

Full file at

IC Language Specification

Typescript on LLVM Language Reference Manual

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

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

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.

Introduction to Programming Using Java (98-388)

Computational Expression

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

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

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

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Full file at

Chapter 2, Part I Introduction to C Programming

The Decaf language 1

Language Reference Manual simplicity

DaMPL. Language Reference Manual. Henrique Grando

Flow Language Reference Manual

Lecture Set 2: Starting Java

Decaf Language Reference

DINO. Language Reference Manual. Author: Manu Jain

Lecture Set 2: Starting Java

BASIC ELEMENTS OF A COMPUTER PROGRAM

DEMO A Language for Practice Implementation Comp 506, Spring 2018

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

B.V. Patel Institute of BMC & IT, UTU 2014

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

Syntax Intro and Overview. Syntax

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

CS201 Some Important Definitions

Chapter 2 Basic Elements of C++

The PCAT Programming Language Reference Manual

Sprite an animation manipulation language Language Reference Manual

Programming for Engineers Introduction to C

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

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

Programming Languages & Translators. XML Document Manipulation Language (XDML) Language Reference Manual

SFPL Reference Manual

YAGL: Yet Another Graphing Language Language Reference Manual

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

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

CS 297 Report. By Yan Yao

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

CS102: Variables and Expressions

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

egrapher Language Reference Manual

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

RTL Reference 1. JVM. 2. Lexical Conventions

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

6.096 Introduction to C++ January (IAP) 2009

The MaSH Programming Language At the Statements Level

The Warhol Language Reference Manual

Chapter 2. Lexical Elements & Operators

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Language Reference Manual

ARG! Language Reference Manual

Language Reference Manual

COMS W4115 Programming Languages & Translators GIRAPHE. Language Reference Manual

VLC : Language Reference Manual

CS 541 Spring Programming Assignment 2 CSX Scanner

CS143 Handout 03 Summer 2012 June 27, 2012 Decaf Specification

The pixelman Language Reference Manual. Anthony Chan, Teresa Choe, Gabriel Kramer-Garcia, Brian Tsau

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Supplemental Materials: Grammars, Parsing, and Expressions. Topics. Grammars 10/11/2017. CS2: Data Structures and Algorithms Colorado State University

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

SFU CMPT 379 Compilers Spring 2018 Milestone 1. Milestone due Friday, January 26, by 11:59 pm.

JME Language Reference Manual

HAWK Language Reference Manual

GraphQuil Language Reference Manual COMS W4115

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

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

GAWK Language Reference Manual

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

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

COMP 202 Java in one week

Fundamentals of Programming Session 4

A First Program - Greeting.cpp

DEPARTMENT OF MATHS, MJ COLLEGE

Java+- Language Reference Manual

IT 374 C# and Applications/ IT695 C# Data Structures

Tokens, Expressions and Control Structures

c) And last but not least, there are javadoc comments. See Weiss.

Basic Types, Variables, Literals, Constants

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

Transcription:

A Grammar for the C- Programming Language (Version F16) 1 Introduction September 20, 2016 This is a grammar for the Fall 2016 semester s C- programming language. This language is very similar to C and has a lot of features in common with a real-world structured programming language. There are also some real differences between C and C-. For instance the declaration of procedure arguments, the loops that are available, what constitutes the body of a procedure etc. Also because of time limitations this language unfortunately does not have any heap related structures. It would be great to do a lot more but that, I guess we ll save for a second semester of compilers. For the grammar that follows here are the types of the various elements by type font or symbol: Keywords are in this type font. TOKEN CLASSES ARE IN THIS TYPE FONT. Nonterminals are in this type font. The symbol ɛ means the empty string in a CS grammar sense. 1.1 Some Token Definitions letter = a... z A... Z digit = 0... 9 letdig = digit letter ID = letter letdig NUMCONST = digit + CHARCONST = is any representation for a single character by placing that character in single quotes. A backslash is an escape character. Any character preceded by a backslash is interpreted as that character. For example \x is the letter x, \ is a single quote, \\ is a single backslash. There are only two exceptions to this rule: \n is a newline character and \0 is the null character. White space (a sequence of blanks and tabs) is ignored. Whitespace may be required to separate some tokens in order to get the scanner not to collapse them into one token. For example: intx is a single ID while int x is the type int followed by the ID x. The scanner, by its nature, is a greedy matcher. Comments are ignored by the scanner. Comments begin with // and run to the end of the line. 1

All keywords are in lowercase. You need not worry about being case independent since not all lex/flex programs make that easy. 2 The Grammar 1. program declarationlist 2. declarationlist declarationlist declaration declaration 3. declaration vardeclaration fundeclaration recdeclaration 4. recdeclaration record ID { localdeclarations 5. vardeclaration typespecifier vardecllist ; 6. scopedvardeclaration scopedtypespecifier vardecllist ; 7. vardecllist vardecllist, vardeclinitialize vardeclinitialize 8. vardeclinitialize vardeclid vardeclid : simpleexpression 9. vardeclid ID ID [ NUMCONST ] 10. scopedtypespecifier static typespecifier typespecifier 11. typespecifier returntypespecifier RECTYPE 12. returntypespecifier int bool char 13. fundeclaration typespecifier ID ( params ) statement ID ( params ) statement 14. params paramlist ɛ 15. paramlist paramlist ; paramtypelist paramtypelist 16. paramtypelist typespecifier paramidlist 17. paramidlist paramidlist, paramid paramid 18. paramid ID ID [ ] 2

19. statement expressionstmt compoundstmt selectionstmt iterationstmt returnstmt breakstmt 20. compoundstmt { localdeclarations statementlist 21. localdeclarations localdeclarations scopedvardeclaration ɛ 22. statementlist statementlist statement ɛ 23. expressionstmt expression ; ; 24. selectionstmt if ( simpleexpression ) statement if ( simpleexpression ) statement else statement 25. iterationstmt while ( simpleexpression ) statement 26. returnstmt return ; return expression ; 27. breakstmt break ; 28. expression mutable = expression mutable += expression mutable = expression mutable = expression mutable /= expression mutable ++ mutable simpleexpression 29. simpleexpression simpleexpression or andexpression andexpression 30. andexpression andexpression and unaryrelexpression unaryrelexpression 31. unaryrelexpression not unaryrelexpression relexpression 32. relexpression sumexpression relop sumexpression sumexpression 33. relop <= < > >= ==! = 34. sumexpression sumexpression sumop term term 35. sumop + 36. term term mulop unaryexpression unaryexpression 37. mulop / % 38. unaryexpression unaryop unaryexpression factor 39. unaryop? 40. factor immutable mutable 41. mutable ID mutable [ expression ] mutable. ID 3

42. immutable ( expression ) call constant 43. call ID ( args ) 44. args arglist ɛ 45. arglist arglist, expression expression 46. constant NUMCONST CHARCONST true false 3 Semantic Notes The only numbers are ints. There is no conversion or coercion between types such as between ints and bools or bools and ints. There can only be one function with a given name. There is no function overloading. The unary asterisk is the only unary operator that takes an array as an argument. It takes an array and returns the size of the array. The logical operators and and or are NOT short cutting. Although it is easy to do, we have plenty of other stuff to implement. In if statements the else is associated with the most recent if. Expressions are evaluated in order consistent with operator associativity and precedence found in mathematics. Also, no reordering of operands is allowed. A char occupies the same space as an integer or bool. Initialization of variables can only be with expressions that are constant, that is, they are able to be evaluated to a constant at compile time. For this class, it is not necessary that you actually evaluate the constant expression at compile time. But you will have to keep track of whether the expression is const. Type of variable and expression must match (see exception for char arrays below). array and record assignment works. Array and record comparison don t. We just don t have time. Passing of arrays and records are done by reference. Functions cannot return an array or record. Assignments in expressions happen at the time the assignment operator is encountered in the order of evaluation. The value returned is value of the rhs of the assignment. Assignments include the ++ and operator. That is, the ++ and operator do NOT behave as in C or C++. 4

Function return type is specified in the function declaration, however if no type is given to the function in the declaration then it is assumed the function does not return a value. To aid discussion of this case, the type of the return value is said to be void, even though there is no void keyword for the type specifier. Code that exits a procedure without a return returns a 0 for an function returning int and false for a function returning bool and a blank for a function returning char. All variables, functions, and record types must be declared before use. Record types are stored like arrays except indexing is done with an ID. Record types can contain items that are of record type but recursive record definition is not allowed.?n generates a uniform random integer in the range 0 to n 1 with the sign of n attached to the result.?5 is a random number in the range 0 to 4.? 5 is a random number in the range 0 to 4.?0 is undefined.?x for array x gives a random element from the array x. 5

4 An Example of C- Code record point { int x, y; record line { point x, y; int ant(int bat, cat[]; bool dog, elk; int fox) { int gnu, hog[100]; point apoint; line aline; line two[2]; apoint.x = 666; apoint.y = 667; aline.x.x = 1; aline.x.y = 2; aline.y.x = 3; aline.y.y = 4; two[0].x.x = 42; two[1].y.x = 43; gnu = hog[2] = 3**cat; // hog is 3 times the size of array passed to cat if (dog and elk or bat > cat[3]) dog =!dog; else fox++; if (bat <= fox) { while (dog) { static int hog; // hog in new scope hog = fox; dog = fred(fox++, cat)>666; if (hog>bat) break; else if (fox!=0) fox += 7; return (fox+bat*cat[bat])/-fox; // note that functions are defined using a statement int max(int a, b) if (a>b) return a; else return b; 6

7

Table 1: A table of all operators in the language. Note that C- supports = for all types of arrays and records. It does not support relative testing:,, >, < for any arrays or records. Array initialization can not happen for any arrays or records. Record access is done with the dot operator and an ID from the set of ids in the record definition. That is the record type has its own symbol table. Operator Arguments Return Type initialization equal N/A not bool bool and bool,bool bool or bool,bool bool == equal types bool! = equal types bool <= int,int bool < int,int bool >= int,int bool > int,int bool <= char,char bool < char,char bool >= char,char bool > char,char bool = equal types type of lhs += int,int int = int,int int = int,int int /= int,int int int int ++ int int any array int int int? int int int,int int + int,int int int,int int / int,int int % int,int int [ array,int type of lhs 8