Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Syntax and programming grammar question and examples For various language syntax

ID: 3742249 • Letter: S

Question

Syntax and programming grammar question and examples

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, however

Explanation / Answer

An attribute grammar is a formal way to define attributes for the productions of a formal grammar, associating these attributes with values. The evaluation occurs in the nodes of the abstract syntax tree, when the language is processed by some parser or compiler.

The attributes are divided into two groups: synthesized attributes and inherited attributes. The synthesized attributes are the result of the attribute evaluation rules, and may also use the values of the inherited attributes. The inherited attributes are passed down from parent nodes.

In some approaches, synthesized attributes are used to pass semantic information up the parse tree, while inherited attributes help pass semantic information down and across it. For instance, when constructing a language translation tool, such as a compiler, it may be used to assign semantic values to syntax constructions. Also, it is possible to validate semantic checks associated with a grammar, representing the rules of a language not explicitly imparted by the syntax definition.

The most successful of several object-oriented successors of C. It is a large an fairly complex language, in part because it supports both procedural and object-oriented programming. The Standard Template Library (STL) is an important library with common compound data types and operations.

main()
{   std::vector<int> intlist;
    int listlen;
    /* read the length of the list */
    std::cin >> listlen;
    if (listlen > 0 && listlen < 100)
    {   int sum = 0;
        /* read the input into an STL vector */
        for (int counter = 0; counter < listlen; counter++)
        {   int value;
            std::cin >> value;
            intlist.push_back(value);
            sum += value;
        }
        /* compute the average */
        int average = sum / listlen;
        /* write the input values > average */
        for (std::vector<int>::const_iterator it = intlist.begin(); it != intlist.end(); ++it)
            if ((*it) > average)
                std::cout << (*it) << std::endl;
    }
    else
        std::cerr << "Error in input list length" << std::endl;
}