Ministry of Higher Education & Scientific Research University of Baghdad Department of Computer Science

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Ministry of Higher Education & Scientific Research

University of Baghdad
Department of Computer Science

Student’s Names: Ibrahim Hasan Tahir.


Mustafa Kamil Hoaidi.

Title: a Report about ASP.NET.

Course: Second Course.

Stage: Second Stage.

Lecture Name: Compilers.


INTRODUCTION
Hi have you ever noticed that, when you are writing code in a text editor
like VS code, it recognizes things like unmatched braces? And it also
sometimes warns you, with an irritating red highlight, about the
incorrect syntax that you have written?
If not, then think about it. That is after all a piece of code. How can you
write code for such a task? What would be the underlying logic behind
it?
These are the kinds of questions that you will face if you have to write a
compiler for a programming language. Writing a compiler is not an easy
task. It is bulky job that demands a significant amount of time and effort.
All the questions we asked earlier represent a problem that is significant
to compiler design called Syntax Analysis. As the name suggests, the
challenge is to analyze the syntax and see if it is correct or not. This is
where we use Context Free Grammars. A Context Free Grammar is a set
of rules that define a language.
Context Free Grammars or CFGs define a formal language. Formal
languages work strictly under the defined rules and their sentences are
not influenced by the context. And that's where it gets the name context
free.
Languages such as English fall under the category of Informal
Languages since they are affected by context. They have many other
features which a CFG cannot describe.
Even though CFGs cannot describe the context in the natural languages,
they can still define the syntax and structure of sentences in these
languages. In fact, that is the reason why the CFGs were introduced in
the first place.
LITRATURE REVIEW
Terminals: These are the characters that make up the actual content of
the final sentence. These can include words or letters depending on
which of these is used as the basic building block of a sentence.
In our case we will use words as the basic building blocks of our
sentences. So our terminals will include words such as "to", "from",
"the", "car", "spaceship", "kittens" and so on.
Non Terminals: These are also called variables. These act as a sub
language within the language defined by the grammar. Non terminals are
placeholders for the terminals. We can use non terminals to generate
different patterns of terminal symbols.
In our case we will use these Non terminals to generate noun phrases,
verb phrases, different nouns, adjectives, verbs and so on.
Start Symbol: a start symbol is a special non terminal that represents the
initial string that will be generated by the grammar.
Now that we know the terminology let's start learning about the
grammatical rules.
While writing grammar rules, we will start by defining the set of
terminals and a start state. As we learned before, that start symbol is a
non-terminal. This means it will belong to the set of non-terminals.
T: ("Monkey", "banana", "ate", "the")
S: Start state. And the output is
LITRATURE REVIEW
The above grammatical rules may seem somewhat cryptic at first. But if
we look carefully, we can see a pattern that is being generated out of
these rules.
A better way to think about the above rules is to visualize them in the
form of a tree structure. In that tree we can put S in the root and
nounPhrase and verbPhrase can be added as children of the root. We can
proceed in the same way with nounPhrase and verbPhrase too. The tree
will have terminals as its leaf nodes because that is where we end these
derivations.

In the above image we can see that S (a nonterminal) derives two non
terminals NP(nounPhrase) and VP(verbPhrase). In the case of NP, it has
derived two non terminals, Adj and Noun.
If you look at the grammar, NP could also have chosen Adj and
nounPhrase. While generating text, these choices are made randomly.
And finally the leaf nodes have terminals which are written in the bold
text. So if you move from left to right, you can see that a sentence is
formed.
METHODS AND APPLICATIONS
For example:
s -> a b
a -> 'the'
b -> c 'cat'
c -> 'happy'
c -> 'sad'
So “c” will be either ‘happy’ or ‘sad’ thus the result should be
the happy cat
the sad cat
RESULT AND CONCLUSIONS
A context-free grammar (CFG) consists of a set of
productions which allow one to replace a variable
by a string of variables and terminals.
The language of a grammar is the set of strings it
generates.
A language is context-free if there is a CFG for it.
Each string in the language has a leftmost derivation and a derivation
tree.
If these are unique for all strings, then the grammar is
called unambiguous.
REFERENCES
Freecodecamp.org
Google books
Shiffman.net
Wikipedia.org

You might also like