0% found this document useful (0 votes)
2 views3 pages

Workshop 2

The document outlines Workshop No. 2 for Computer Science III, focusing on compilers and regular expressions used in Python. It includes tasks for defining regular expressions for various programming constructs and presents a context-free grammar with production rules for a simple programming language. Additionally, it specifies exercises to create derivation trees for given statements, with a deadline for submission.

Uploaded by

munozdaniela11a
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)
2 views3 pages

Workshop 2

The document outlines Workshop No. 2 for Computer Science III, focusing on compilers and regular expressions used in Python. It includes tasks for defining regular expressions for various programming constructs and presents a context-free grammar with production rules for a simple programming language. Additionally, it specifies exercises to create derivation trees for given statements, with a deadline for submission.

Uploaded by

munozdaniela11a
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/ 3

Computer Science III

2024-III
WorkShop No. 2 — Compilers

Eng. Carlos Andrés Sierra, M.Sc.


Computer Engineering
Universidad Distrital Francisco José de Caldas

1. For each one of next cases definr a regular expression as used in a compiler based on
the Python re library:

(i) Identifier: A regular expression to match valid identifiers (variable names, func-
tion names, etc.).
(ii) Integer Literal: A regular expression to match integer literals.
(iii) Floating Point Literal: A regular expression to match floating-point literals.
(iv) String Literal: A regular expression to match string literals enclosed in double
quotes.
(v) Single-line Comment: A regular expression to match single-line comments
starting with ‘//‘.
(vi) Multi-line Comment: A regular expression to match multi-line comments
enclosed in ‘/* */‘.
(vii) Whitespace: A regular expression to match whitespace characters (spaces, tabs,
newlines).
(viii) Operators: A regular expression to match common operators (e.g., ‘+‘, ‘-‘, ‘*‘,
‘/‘, ‘==‘, ‘!=‘).
(ix) Keywords: A regular expression to match reserved keywords (e.g., ‘if‘, ‘else‘,
‘while‘, ‘return‘).
(x) Hexadecimal Literal: A regular expression to match hexadecimal literals.

2. Be G a context-free grammar with the following productions:

Carlos Andrés Sierra, Computer Engineer, M.Sc. on Computer Engineering, Titular Professor at Uni-
versidad Distrital Francisco José de Caldas.
Any comment or concern related to this document could be send to Carlos A. Sierra at e-mail: cavir-
[email protected]
COMPUTER SCIENCE III — WORKSHOP NO. 2 2

S −> Program
Program −> S t a t e m e n t L i s t
S t a t e m e n t L i s t −> Statement S t a t e m e n t L i s t | <lambda>
Statement −> Assignment | I f S t a t e m e n t | WhileStatement | ReturnStatement
Assignment −> I d e n t i f i e r "=" E x p r e s s i o n " ; "
I f S t a t e m e n t −> " i f " " ( " E x p r e s s i o n " ) " " { " S t a t e m e n t L i s t " } " E l s e P a r t
E l s e P a r t −> " e l s e " " { " S t a t e m e n t L i s t " } " | <lambda>
WhileStatement −> " w h i l e " " ( " E x p r e s s i o n " ) " " { " S t a t e m e n t L i s t " } "
ReturnStatement −> " r e t u r n " E x p r e s s i o n " ; "
E x p r e s s i o n −> Term E x p r e s s i o n ’
E x p r e s s i o n ’ −> "+" Term E x p r e s s i o n ’ | " −" Term E x p r e s s i o n ’ |
<lambda>
Term −> F a c t o r Term ’
Term ’ −> " ∗ " F a c t o r Term ’ | " / " F a c t o r Term ’ | <lambda>
F a c t o r −> " ( " E x p r e s s i o n " ) " | I d e n t i f i e r | Number
I d e n t i f i e r −> [ a−zA−Z_ ] [ a−zA−Z0−9_] ∗
Number −> [0 −9]+

Explanation:

• S is the start symbol.


• Program consists of a list of statements.
• StatementList is a sequence of statements or an empty sequence (< lambda >).
• Statement can be an assignment, an if statement, a while statement, or a return
statement.
• Assignment assigns an expression to an identifier.
• IfStatement includes an optional else part.
• WhileStatement represents a while loop.
• ReturnStatement returns an expression.
• Expression consists of terms combined with addition or subtraction.
• Term consists of factors combined with multiplication or division.
• Factor can be an expression in parentheses, an identifier, or a number.
• Identifier matches typical variable names.
• Number matches sequences of digits.

Based on the provided context-free grammar, create derivation trees for the following
statements:

(a) Exercise 1:
x = 5 + 3 ∗ 2;

(b) Exercise 2:
COMPUTER SCIENCE III — WORKSHOP NO. 2 3

i f ( x > 0) {
y = x − 1;
} else {
y = 0;
}

(c) Exercise 3:
while ( x < 10) {
x = x + 1;
}

(d) Exercise 4:
return (a + b) ∗ c ;

Deadline: Saturday, 8th of February, 2025, 14:00 (local time).

You might also like