0% found this document useful (0 votes)
89 views6 pages

Design Language For Compiler

This document contains information about the design language for a compiler project in Python, including definitions of tokens, keywords, operators, constants, identifiers, punctuators, and classifications. It also provides the syntax definitions for common programming constructs in Python like if/else statements, loops, functions, classes, exceptions, and more.

Uploaded by

Saira Awan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views6 pages

Design Language For Compiler

This document contains information about the design language for a compiler project in Python, including definitions of tokens, keywords, operators, constants, identifiers, punctuators, and classifications. It also provides the syntax definitions for common programming constructs in Python like if/else statements, loops, functions, classes, exceptions, and more.

Uploaded by

Saira Awan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIVERSITY OF KARACHI

DEPARTMENT OF COMPUTER SCIENCE


(UBIT)
COURSE 504
COMPILER CONSTRUCTION
PROJECT FILE
BSCS 3RD YEAR EVENING SECTION A

SAIRA AWAN EP1849093


SYED AREEB ALI EP1849106
S.M HAMZA EP1849091

TABLE OF CONTENTS

S# TOPIC Date
DESIGN LANGUAGE FOR COMPILER
With reference to python
TOKENS
( Smallest individual unit in a program is known as token. )

Keyword Description
And A logical operator
As To create an alias
Assert For debugging
Break To break out of a loop
Class To define a class
Continue To continue to the next iteration of a loop
def To define a function
Del To delete an object
Elif Used in conditional statements, same as else if
Else Used in conditional statements
Except Used with exceptions, what to do when an exception occurs
False Boolean value, result of comparison operations
Finally Used with exceptions, a block of code that will be executed
no matter if there is an exception or not
For To create a for loop
From To import specific parts of a module
Global To declare a global variable
If To make a conditional statement
Import To import a module
In To check if a value is present in a list, tuple, etc.
Is To test if two variables are equal
Lambda To create an anonymous function
None Represents a null value
Nonlocal To declare a non-local variable
Not A logical operator
Or A logical operator
Pass A null statement, a statement that will do nothing
Raise To raise an exception
Return To exit a function and return a value
True Boolean value, result of comparison operations
Try To make a try...except statement
While To create a while loop
With Used to simplify exception handling
yield To end a function, returns a generator

Operators Constants
Arithmetic operators Integer
Assignment operators Float
Relational Operators String
Bitwise operators Boolean
Logical operators Character
Increment / Decrement
Identifiers

Identifier is a name used to identify a variable, function, class, module or other


object.
 An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero
or more letters, underscores and digits (0 to 9).
 special characters are not allowed
 Identifier must not be a keyword .
 case sensitive programming language. Thus, Rollnumber and rollnumber are two
different identifiers .
 Some valid identifiers : Mybook, file123, z2td, date_2, _no
 Some invalid identifier : 2rno,break,my.book,data-cs
Some additional naming conventions
 Class names start with an uppercase letter. All other identifiers start with a lowercase
letter.
 Starting an identifier with a single leading underscore indicates that the identifier is
private.
 Starting an identifier with two leading underscores indicates a strong private identifier.
 If the identifier also ends with two trailing underscores, the identifier is a language-
defined special name.

Punctuators

Used to implement the grammatical and structure of a Syntax.Following are some


punctuators.

: ; . , ( ) { } [ ] “ ‘ ::
With reference to python
CLASSIFICATION
( Lexemes that are syntactically replaceable and have same precedence would be placed in same class )

Class Name Lexeme(s)


Arithmetic Operators + -
MDM operators * / % //
Relational Operators < > <= >= != ==
Exponentiation **
Assignment operators += -= *= /= %= //= **=
Special Assignment operator =
Logical Operators And && or ||
Special Logical Operator ! not
Increment / Decrement ++ --
Data Types Int float
List / Array List / Array
Tuple Tuple
string string
set set
Public access modifier Public
Special access modifier Protected Private
For For
While While
Inside Loop body Break Continue

Else Else
If If
Switch Switch
Exception Try , except , else
Case Switch
Class Class
Object Object
Method Method
Inheritance Inheritance
Encapsulation Encapsulation
Polymorphism Polymorphism
Regular Expression
Identifiers :
“ ^ ( - | A-Z a-z ) + [ a – z A – Z 0 – 9 _ ] * [ a – z A – Z 0 – 9 ] * [ a – z A – Z ] $ “
Integer Constant : “ ^ ( - | + ) ? [ 0 – 9 ] + $ “
Float Constant : “ ^ ( - | + ) ? [ 0 – 9 ] * . [ 0 – 9 ] * $ “
Char Constant :“^‘(\(A|C))|(B)|(C)‘$“
A = Characters with \ { | , “ }
B = without backlash \ { @ , # , + … }
C = with or without backlash { b , a , t , o }
String Constant : “ ^ “ ( \ ( A|C) ) | (B ) | ( C ) “ $ “
SYNTAX
If statement ( Python ) If else statement ( Python )
if expression: if expression:
statement(s) statement(s)
else:
statement(s)
While loop ( Python ) For Loop ( Python )
while expression: for iterating_var in sequence:
statement(s) statements(s)
List / Array Tuple ( Python )
Variable = [ ‘ item ‘ , item ] tup1 =('physics', 'chemistry );
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";
Exception ( Python ) Class
try:
You do your operations here; class ClassName:
...................... 'Optional class documentation
except ExceptionI: string'
If there is ExceptionI, then class_suite
execute this block.
except ExceptionII:
If there is ExceptionII, then Object
execute this block.
...................... Variable = class( )
else:
If there is no exception then
execute this block.

Assert ( Python ) Method ( Python )


assert Expression[, Arguments] Inheritance ( Python )
class BaseClass:
Body of base class
class DerivedClass(BaseClass):
Body of derived class
Encapsulation ( Python ) Polymorphism ( Python )
Set ( Python ) Lambda ( Python )
Variable = {“item” , “item” } lambda arguments : expression
Switch Import
switch( expression ) import module_name
{ case value-1:
Block-1;
Break;

case value-n:
Block-n;
Break;
default:
Block-1;
Break;
}
Statement-x;

You might also like