0% found this document useful (0 votes)
14 views

Lang Spec

The document describes a language specification that includes: 1. Data types and regular expressions to define integers, floats, strings, and identifiers. 2. Operators for arithmetic, comparison, logical expressions, and symbols for assignment, delimiters, parentheses, braces and brackets. 3. Context-free grammar rules for variables, loops, conditional statements, and other language elements.

Uploaded by

Abdul Mannan
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)
14 views

Lang Spec

The document describes a language specification that includes: 1. Data types and regular expressions to define integers, floats, strings, and identifiers. 2. Operators for arithmetic, comparison, logical expressions, and symbols for assignment, delimiters, parentheses, braces and brackets. 3. Context-free grammar rules for variables, loops, conditional statements, and other language elements.

Uploaded by

Abdul Mannan
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/ 5

LANGUAGE SPECIFICATION

Regular Expressions:
• integer = [+-]?\d+

• float = [+-]?\d+(\.\d+)?([eE][+-]?\d+)?

• string = "[^"\\]*(\\.[^"\\]*)*"

• id = [a-zA-Z][a-zA-Z0-9_]*

Operators:

• Arithmetic: + , - , * , / , %
• Comparison: == , != , < , > , <= , >=
• Logical: && , || , !

R.E: (\|\||&&|==|!=|<|>|<=|>=|\+|-|\*|\/|%|!)

Symbols:

• Assignment: =
• Delimiters: , , ;
• Parentheses: ( , )
• Braces: { , }
• Square Brackets: [ , ]

R.E: (\=|\,|\;|\(|\)|\{|\}|\[|\])

Production Rules for CFG:


<program> → <statement>*
<statement> → = <variable_declaration> | <assignment> | <loop> |
<conditional_statement>
<variable_declaration> → <data_type> <identifier> ';'
<assignment> → <identifier> '=' <expression> ';'
<loop> → <for_loop> | <while_loop>
<for_loop> → 'for' '(' <assignment> ';' <expression> ';' <assignment>
')' <block>
<while_loop> → 'while' '(' <expression> ')' <block>
<conditional_statement> → 'if' '(' <expression> ')' <block> ['else'
<block>]
<block> → '{' <statement>* '}'
<expression> → <identifier> | <literal> | <binary_expression>
<binary_expression> → <expression> <binary_operator> <expression>
<binary_operator> → operators
<literal> → <integer_literal> | <float_literal> | <string_literal> |
<boolean_literal>
<integer_literal> → integer
<float_literal> → float
<string_literal> → string
<boolean_literal> → 'true' | 'false'
<identifier> → id
<data_type> → 'int' | 'float' | 'string' | 'boolean'

1. Variables with Data Types:


language supports variables declaration and assignment with data types.
We can use this feature to define variables in their code with a specific
data type, assign them values using expressions, and then use those
variables elsewhere in their program. The context-free grammar rules for
variables are designed to ensure that variables are defined and used
correctly, and that the type of data assigned to a variable matches its
declared data type.
Data Types
• The language supports the following primitive data types:
• Integer
• Float
• Boolean
• String
• Character

Variable Declaration and Assignment


• Variables must be declared and assigned a value before they can be
used.
• The syntax for variable declaration and assignment is as follows:

Syntax:
<variable_declaration> → <data_type> <variable_name> "=" <expression> ";"

Rules:
<variable_declaration> -> <data_type> <variable_name> "=" <expression>
";"
<data_type> -> "int" | "float" | "string" | "bool"
<variable_name> -> <identifier>
<expression> -> <literal> | <identifier>
<literal> -> <int_literal> | <float_literal> | <string_literal> |
<bool_literal>
<int_literal> -> integer
<float_literal> -> float
<string_literal> -> string
<bool_literal> -> "true" | "false"
<identifier> -> id

Example:
string message = "Hello, world!";

Derivation:
<variable_declaration> -> <data_type> <variable_name>"="<expression>";"
-> "string" <variable_name>"="<expression> ";"
-> "string" "message" "=" <expression> ";"
-> "string" "message" "=" <literal> ";"
-> "string" "message" "=" <string_literal> ";"
-> "string" "message" "=" ""Hello, world!"" ";"

Parse Tree:

variable_declaration
/ | | \
data_type | | expression ;
/ | / | \
string message = "Hello, world!" ;

2. Loops
(a). For Loop:
language supports for loop control structures, which are used to repeat a
block of code a specific number of times. we can use this feature to
iterate over a set of values, or to perform a specific action a certain
number of times. The context-free grammar rules for for loops ensure
that the loop is initialized, updated, and terminated correctly, and that
the loop executes the correct number of times.

Syntax:
<for_loop> -> "for" "(" <for_initializer> ";" <for_condition> ";"
<for_update> ")" <statement>
<for_initializer> -> <variable_declaration> | <expression>
<for_condition> -> <boolean_expression>
<for_update> -> <expression>

Rules:
<statement> -> <expression> ";" | <block> | <if_statement> | <while_loop>
| <for_loop> | <return_statement> | <break_statement> |
<continue_statement> | <function_call>
<block> -> "{" <statement>* "}"
<if_statement> -> "if" "(" <boolean_expression> ")" <statement> [ "else"
<statement> ]
<for_loop> -> "for" "(" <for_initializer> ";" <for_condition> ";"
<for_update> ")" <statement>
<for_initializer> -> <variable_declaration> | <expression>
<for_condition> -> <boolean_expression>
<for_update> -> <expression>

Example:
for (int i = 0; i < 10; i++) {
print(i);
}

Derivation:
<for_loop> -> "for" "(" <for_initializer> ";" <for_condition> ";" <for_update>
")" <statement>
-> "for" "(" <variable_declaration> ";" <boolean_expression> ";"
<expression> ")" <statement>
-> "for" "(" "int" <variable_name> "=" <expression> ";"
<boolean_expression> ";" <expression> ")" <statement>
-> "for" "(" "int" "i" "=" <expression> ";" <boolean_expression>
";" <expression> ")" <statement>
-> "for" "(" "int" "i" "=" <literal> ";" <boolean_expression> ";"
<expression> ")" <statement>
-> "for" "(" "int" "i" "=" "0" ";" <boolean_expression> ";"
<expression> ")" <statement>
-> "for" "(" "int" "i" "=" "0" ";" "i" "<" <expression> ";"
<expression> ")" <statement>
-> "for" "(" "int" "i" "=" "0" ";" "i" "<" "10" ";" <expression>
")" <statement>
-> "for" "(" "int" "i" "=" "0" ";" "i" "<" "10" ";" "i" "++" ")"
<statement>
-> "for" "(" "int" "i" "=" "0" ";" "i" "<" "10" ";" "i" "++" ")"
"{" <statement> "}"

Parse Tree:
for_loop
/ | | \
for ( ) ; statement
/ | \ |
for_initializer | for_condition | for_update
/ | \
variable_declaration boolean_expression expression
/ | / \
data_type variable_name < expression > ;
/ | | |
int i = 0
/ | \
identifier < operator > <operand>
/ |
literal identifier
| |
0 I

3. Conditional Statement:
language supports conditional statements (if-else statements), which are
used to execute different blocks of code depending on the value of a
boolean expression. we can use this feature to control the flow of their
program based on specific conditions. The context-free grammar rules for
conditional statements ensure that the boolean expression is evaluated
correctly, and that the correct block of code is executed based on the
value of that expression.

Syntax:
<if_statement> -> "if" "(" <boolean_expression> ")" <statement> [ "else"
<statement> ]

Rules:
<if_statement> -> "if" "(" <boolean_expression> ")" <statement> [ "else"
<statement> ]
<boolean_expression> -> <expression> <relational_operator> <expression>
<relational_operator> -> "==" | "!=" | "<" | "<=" | ">" | ">="

Example:
if (x > 0) {
print("Positive");
} else {
print("Non-positive");
}

Derivation:
<if_statement> -> "if" "(" <boolean_expression> ")" <statement> [ "else"
<statement> ]
-> "if" "(" <operand> <relational_operator> <operand> ")"
<statement> [ "else" <statement> ]
-> "if" "(" identifier <relational_operator> <operand> ")"
<statement> [ "else" <statement> ]
-> "if" "(" identifier ">" <operand> ")" <statement> [ "else"
<statement> ]
-> "if" "(" "x" ">" <operand> ")" <statement> [ "else"
<statement> ]
-> "if" "(" "x" ">" "0" ")" "{" <statement> "}" "else" "{"
<statement> "}"

Parse Tree:

if_statement
/ | \
if ( ) statement
/ | \
operand rel_op block
/ | |
identifier > <statement>
/ | \ |
literal x { statement
|
<expression>
|
print()
|
literal "Positive"

You might also like