Unformatted text preview:

Arithmetic Calculation Language Nathan Corvino May 12 2011 1 Introduction 5 1 1 Data Types 5 1 2 Operators 5 1 3 Control Structures 5 1 4 Functions 5 1 5 print 5 1 5 Example 5 2 Tutorial 7 2 1 An First Example 7 2 2 Compiling and Running 7 2 3 Another Example 8 3 Language Reference Manual 9 3 1 Introduction 9 3 2 Lexical Conventions 9 3 2 1 Comments 9 3 2 2 Identifiers 9 3 2 3 Keywords 9 3 2 4 Constants 9 3 3 Variable Declarations 10 3 4 Function Definitions 10 3 4 1 print 11 3 5 Expressions 11 3 5 1 Function Calls 11 3 5 2 Variable Access 11 3 5 3 Multiplicative Operators 12 3 5 4 Additive Operators 12 3 5 5 Relational Operators 12 3 5 6 Equality Operators 12 3 5 7 Assignment 12 3 6 Statements 12 3 6 1 if Statement 13 3 6 2 while Statement 13 3 6 3 return Statement 13 4 Project Plan 14 4 1 Planning Specification and Development and Testing process 14 4 2 Coding Style 14 4 3 Development Tools 15 4 4 Project Log 15 5 Architectural Design 5 1 Components 6 Test Plan 16 16 17 6 1 Examples 17 6 2 Automation 22 6 3 Test Suite 24 7 Lessons Learned 30 A Grammar 31 B Code Listing 33 B 1 scanner ml 33 B 2 parser mly 34 B 3 compile ml 36 B 4 acl ml 42 B 5 Makefile 42 1 Introduction The Arithmetic Calculation Language ACL focuses on arithmetic operations combined with common procedural control structures It s primary motivation is to explore assembly code generation as such it is primarily focused on remaining simple to keep this task tractable At the same time it does strives to explore as many different code constructs as feasible It includes variables conditionals loops and function calls The syntax is c like Statements are terminated with a semicolon and a list of statements can be enclosed in brackets 1 1 Data Types ACL supports ints and arrays of ints 1 2 Operators Addition subtraction multiplication and division of integers is supported Comparison operators and compare int values and return 1 if the comparison is true 0 if false 1 3 Control Structures ACL supports while loops and if else structures 1 4 Functions Function calls are be supported and the number of parameters in the definition and call has to match There is no limit on the number of parameters for a function Functions return ints The program entry point is the main method which returns an int Passing command line arguments to it is not be supported 1 5 print ACL has a built in print function that prints an int with a newline to standard output 1 5 Example An example program that illustrates these constructs in action abs int number if number 0 return 1 number else return number power int base int exponent int total total 1 while exponent 0 total base total exponent exponent 1 return total int main int x int y x 4 y 3 print power x abs y This sample program returns 64 when compiled by gcc as expected it does the same when compiled by the ACL compiler This example demonstrates comparisons looping branching and function calls 2 Tutorial The Arithmetic Calculation Language ACL is a simple procedural language for manipulating integers Its is syntax is c like although greatly simplified Programs start execution in a main function and output results using the built in print function performing integer calculations with standard operators 2 1 An First Example int main int x int y x 4 y 3 print x print x print x print x y y 1 2 The above program proceeds by assigning values to the two variables defined x and y and then performing simple math with them It outputs 1 12 3 2 2 2 Compiling and Running ACL generates assembly code for the GNU Linux assembler Furthermore it depends on the standard c libraries so the linker must be told where to load them To compile and run a program test acl the following commands would be executed acl c test acl test s as o test o test s ld dynamic linker lib ld linux so 2 o test lc test o test The first command uses ACL to generate the assembly code the c flag indicates compile ACL prints its output to standard output which is piped to test s Next the assembler is run on test s production and object file test o This is linked using the linker specifying the location of the dynamic linker and output the test executable Finally that is run 2 3 Another Example A more complicated example which illustrates a lot of the features of ACL calculates whether a number is prime prime int int int int a i multiplier result i a 2 while i 2 multiplier a i result i multiplier if result a return 0 i i 1 return 1 main print prime 7 print prime 37 print prime 42 print prime 45 ACL does not include remainder division so a search for divisors proceeds by trying to divide then multiply the division result by all the integers between 3 and half of the argument to prime If the result of dividing and re multiplying produces the starting number then a divisor is found and 0 is returned Otherwise 1 is returned A while loop is used to try each number subtracting one each time through the loop 3 Language Reference Manual 3 1 Introduction This reference manual describes the Arithmetic Calculation Language ACL a small subset of C that allows numeric calculations to be done in a procedural fashion The language only allows integer values to be manipulated hence the name Arithmetic Calculation Language It does however allow for arrays of integers to be declared and provides a language features that are illustrative of the core procedural constructs variable declarations function definitions selection and iteration Ints in ACL are 32 bits it generates 32 bit Linux assembly code and the data values are handled by the machine No checks are done for overflow or underflow the behavior of programs which produce such results is undefined 3 2 Lexical Conventions A program consists of a single file containing global variable declarations and procedure definitions Execution begins with the function named main which is required 3 2 1 Comments Comments are introduced by or Comments introduced by are terminated with while comments introduced by terminate at the next newline 3 2 2 Identifiers Identifiers consist of letters digits and underscores and must begin with a letter or underscore case is significant 3 2 3 Keywords The following identifiers are used as keywords and may not be used as identifiers for functions or variables if else while int return 3 2 4 Constants Integer constants consist of a series of digits and are treated as decimal numbers 3 3 Variable Declarations Variables are introduced by their type the only allowed type is int


View Full Document

Columbia COMS W4115 - Arithmetic Calculation Language

Documents in this Course
YOLT

YOLT

13 pages

Lattakia

Lattakia

15 pages

EasyQL

EasyQL

14 pages

Photogram

Photogram

163 pages

Espresso

Espresso

27 pages

NumLang

NumLang

6 pages

EMPATH

EMPATH

14 pages

La Mesa

La Mesa

9 pages

JTemplate

JTemplate

238 pages

MATVEC

MATVEC

4 pages

TONEDEF

TONEDEF

14 pages

SASSi

SASSi

16 pages

JTemplate

JTemplate

39 pages

BATS

BATS

10 pages

Synapse

Synapse

11 pages

c.def

c.def

116 pages

TweaXML

TweaXML

108 pages

Load more
Loading Unlocking...
Login

Join to view Arithmetic Calculation Language and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Arithmetic Calculation Language and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?