syntax and grammar questions. please use your own words and no examples already
ID: 3743790 • Letter: S
Question
syntax and grammar questions. please use your own words and no examples already online!
For various language syntax forms in programming languages, how to distinguish them in grammar? You can use examples to illustrate the grammars and syntax forms of interest. If you already addressed this question last week, you may skip the question) Why attribute grammar? How is it applied when implementing a programming language? Use a simple example toillistrate the use of atibue grammar in progamning languages. Please do not use the textbook examples, howeverExplanation / Answer
Each and every programming language usually has it's own form of grammar. The grammar of the language is what makes sense to the compiler when we compile a piece of code. It all depends on how the compiler has been coded to go about parsing the code and to make sense of it. Most of the times, the differences in grammar between 2 programming language becomes evident in the syntax of the program itself.
For example:
In C++, int a=5; means we want to assign an integer value 5 to an integer variable a
In Python, we just write a=5, meaing the same exact thing, but here it is the duty of the compiler to detect the corresponding datatye of the variable being declared.
Attribute grammar is required in order to achive efficiency. We want our compilers to be fast. If we use attribute grammar, we can use data strucutres like a directed acyclic graph in the compiler for parsing. This takes care of the duplicate values appearing in the code.
For example: writing int a=5; in C++followed by another statement a few lines later like a=100; makes the compiler understand that we are talking about the very same variable, and that just it's value is to be updated. This means that just the value of the attribute a has to be updated.
Giving the variable a the status of an attribute protects us from falling into chaos in the sense that the compiler knows that we are talking about the same attribute and not a different one. This is achieved through maintaining a table in the memory, where all the attributes, thier values, their statuses, datatypes and other important information is stored.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.