A Grammar for the C- Programming Language (Version S16) March 12, 2016



Similar documents
C Compiler Targeting the Java Virtual Machine

Chapter 2: Elements of Java

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Programming Languages CIS 443

Stacks. Linear data structures

Intermediate Code. Intermediate Code Generation

Moving from CS 61A Scheme to CS 61B Java

CS 106 Introduction to Computer Science I

Chapter 2 Introduction to Java programming

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Introduction to Java

Sources: On the Web: Slides will be available on:

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

C++ INTERVIEW QUESTIONS

Pemrograman Dasar. Basic Elements Of Java

JavaScript: Control Statements I

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

1 Abstract Data Types Information Hiding

J a v a Quiz (Unit 3, Test 0 Practice)

Variables, Constants, and Data Types

Chapter One Introduction to Programming

Lexical analysis FORMAL LANGUAGES AND COMPILERS. Floriano Scioscia. Formal Languages and Compilers A.Y. 2015/2016

Programming Project 1: Lexical Analyzer (Scanner)

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

VB.NET Programming Fundamentals

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

PL / SQL Basics. Chapter 3

Introduction to Python

Compiler Construction

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

CS106A, Stanford Handout #38. Strings and Chars

Chapter 3 Operators and Control Flow

Lecture 1 Introduction to Java

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. CSC467 Compilers and Interpreters Fall Semester, 2005

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Semantic Analysis: Types and Type Checking

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Chapter 2 Basics of Scanning and Conventional Programming in Java

Glossary of Object Oriented Terms

Compiler I: Syntax Analysis Human Thought

Object Oriented Software Design

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

13 Classes & Objects with Constructors/Destructors

Install Java Development Kit (JDK) 1.8

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Python Lists and Loops

Informatica e Sistemi in Tempo Reale

So far we have considered only numeric processing, i.e. processing of numeric data represented

Compiler Construction

Semester Review. CSC 301, Fall 2015

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

CS 111 Classes I 1. Software Organization View to this point:

C++ Language Tutorial

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Answers to Review Questions Chapter 7

CSCI 3136 Principles of Programming Languages

Example of a Java program

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

MISRA-C:2012 Standards Model Summary for C / C++

Lecture 5: Java Fundamentals III

AP Computer Science Java Subset

Member Functions of the istream Class

The Cool Reference Manual

Computer Programming I

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Programming languages C

Explain the relationship between a class and an object. Which is general and which is specific?

Writing Control Structures

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Flex/Bison Tutorial. Aaron Myles Landwehr CAPSL 2/17/2012

Number Representation

In this Chapter you ll learn:

MASSACHUSETTS INSTITUTE OF TECHNOLOGY PROJECT MAC. Memorandum-M-352 July 21, 1967 ABSTRACT

Introduction to Python

Part I. Multiple Choice Questions (2 points each):

CS107L Handout 02 Autumn 2007 October 5, 2007 Copy Constructor and operator=

Introduction to Lex. General Description Input file Output file How matching is done Regular expressions Local names Using Lex

Chapter 5 Functions. Introducing Functions

Visual Basic Programming. An Introduction

Python Evaluation Rules

Objective-C Tutorial

Passing 1D arrays to functions.

Unix Scripts and Job Scheduling

Lecture 9. Semantic Analysis Scoping and Symbol Table

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Creating Basic Excel Formulas

Transcription:

A Grammar for the C- Programming Language (Version S16) 1 Introduction March 12, 2016 This is a grammar for this 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 + STRINGCONST = any series of zero or more characters enclosed by double quotes. A backslash is an escape character. Any character preceded by a backslash is interpreted as that character without meaning to the string syntax. For example \x is the letter x, \" is a double quote, \ 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. The string constant can be an empty string: a string of length 0. All string constants are terminated by the first unescaped double quote. String constants must be entirely contained on a single line, that is, they contain no unescaped newlines! CHARCONST = is any representation for a single character in a string constant. All the rules for a STRINGCONST apply to a CHARCONST with the substitution of enclosing single quotes rather that double quotes and the length of the string is exactly one character. 1

White space (a sequence of blanks and tabs) is ignored except that it must separate tokens such as ID s, NUMCONST s, and keywords. Comments are ignored by the scanner. Comments begin with // and run to the end of the line. 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 funcdeclaration 4. vardeclaration typespecifier vardecllist ; 5. scopedvardeclaration scopedtypespecifier vardecllist ; 6. vardecllist vardecllist, vardeclinitialize vardeclinitialize 7. vardeclinitialize vardeclid vardeclid : simpleexpression 8. vardeclid ID ID [ NUMCONST ] 9. scopedtypespecifier static typespecifier typespecifier 10. typespecifier int bool char 11. funcdeclaration typespecifier ID ( params ) statement ID ( params ) statement 12. params paramlist ɛ 13. paramlist paramlist ; paramtypelist paramtypelist 14. paramtypelist typespecifier paramidlist 15. paramidlist paramidlist, paramid paramid 16. paramid ID ID [ ] 2

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

40. immutable ( expression ) call constant 41. call ID ( args ) 42. args arglist ɛ 43. arglist arglist, expression expression 44. constant NUMCONST CHARCONST STRINGCONST 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 STRINGCONST token translates to a fixed size char array. The logical operators & and are NOT short cutting 1. 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. A string is a constant char array. 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). 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 ++ operator. That is, the ++ operator does NOT behave as it does in C or C++. 1 Although it is easy to do, we have plenty of other stuff to implement. 4

Assignment of a string (char array) to a char array. This simply assigns all of the chars in the rhs array into the lhs array. It will not overrun the end of the lhs array. If it is too short it will pad the lhs array with null characters which are equivalent to zeroes. The initializing a char array to a string behaves like an array assignment to the whole array. 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 must be declared before use and functions must be defined before use.?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[? x] give a random element from the array x. 5

The foreach loop construct has a mutable and a simple-expression in the parentheses. If the type of simple-expression is an array then the mutable must be the same type but a non-array and the foreach will loop through all the elements of the array putting each in turn into the mutable. If simple-expression is an not an array then then then the mutable must be of type int and it will be assigned the values from 0 to the size of the array minus one i.e. it goes through all the indexes of the array. Here is the meaning of the two types of foreach. Note the use of a new scope to hide local variables and compute the loop bounds once if y is an expression. // THE MEANING OF: foreach (x in y) stuff; // if y is a scalar means { int p, limit; p = 0; limit = y; while (p<limit) { x = p; stuff; p++; // THE MEANING OF: foreach (x in ya) stuff; // if ya is an array it means { int p, limit; p = 0; limit = *ya; while (p<limit) { x = ya[p]; stuff; p++; // this changes from above by *ya // this changes from above by indexing the array 6

4 An Example of C- Code int ant(int bat, cat[]; bool dog, elk; int fox) { int gnu, hog[100]; gnu = hog[2] = 3**cat; // hog is 3 times the size of array passed to cat if (dog & elk 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; 7

Table 1: A table of all operators in the language. Note that C- supports ==,! = and = for all types of arrays. It does not support relative testing:,, >, < for any arrays. Array initialization can only happen for the char type since the only array constant available in C- is char (a string). Operator Arguments Return Type initialization equal,string N/A! bool bool & bool,bool bool 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 same as lhs operand += 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 8