if you want to remove an article from website contact us from top.

    dangling else ambiguity in compiler design

    Mohammed

    Guys, does anyone know the answer?

    get dangling else ambiguity in compiler design from screen.

    Dangling else

    Dangling else

    From Wikipedia, the free encyclopedia

    Jump to navigation Jump to search

    The dangling else is a problem in programming of parser generators in which an optional else clause in an if–then(–else) statement results in nested conditionals being ambiguous. Formally, the reference context-free grammar of the language is ambiguous, meaning there is more than one correct parse tree.

    In many programming languages one may write conditionally executed code in two forms: the if-then form, and the if-then-else form – the else clause is optional:

    if a then sif b then s1 else s2

    This gives rise to an ambiguity in interpretation when there are nested statements, specifically whenever an if-then form appears as s1 in an if-then-else form:

    if a then if b then s else s2

    In this example, s is unambiguously executed when a is true and b is true, but one may interpret s2 as being executed when a is false (thus attaching the else to the first if) or when a is true and b is false (thus attaching the else to the second if). In other words, one may see the previous statement as either of the following expressions:

    if a then (if b then s) else s2if a then (if b then s else s2)

    The dangling else problem dates to ALGOL 60,[1] and has been resolved in various ways in subsequent languages. In LR parsers, the dangling else is the archetypal example of a shift-reduce conflict.

    Contents

    1 Avoiding ambiguity while keeping the syntax

    2 Avoiding ambiguity by changing the syntax

    3 Examples 3.1 C

    4 Avoiding the conflict in LR parsers

    5 See also 6 References

    Avoiding ambiguity while keeping the syntax[edit]

    This is a problem that often comes up in compiler construction, especially scannerless parsing. The convention when dealing with the dangling else is to attach the else to the nearby if statement,[2] allowing for unambiguous context-free grammars, in particular. Programming languages like Pascal,[3] C[4] and Java[5] follow this convention, so there is no ambiguity in the semantics of the , though the use of a parser generator may lead to ambiguous . In these cases alternative grouping is accomplished by explicit blocks, such as begin...end in Pascal[6] and {...} in C.

    Depending on the compiler construction approach, one may take different corrective actions to avoid ambiguity:

    If the parser is produced by an SLR, LR(1) or LALR LR parser generator, the programmer will often rely on the generated parser feature of preferring shift over reduce whenever there is a conflict.[2] Alternatively, the grammar can be rewritten to remove the conflict, at the expense of an increase in grammar size (see below).

    If the parser is hand written, the programmer may use a context-free grammar. Alternatively, one may rely on a non-context-free grammar or a parsing expression grammar.

    Avoiding ambiguity by changing the syntax[edit]

    The problem can also be solved by making explicit the link between an else and its if, within the syntax. This usually helps avoid human errors.[7]

    Possible solutions are:

    Having an "end if" symbol delimiting the end of the if construct. Examples of such languages are ALGOL 68, Ada, Eiffel, PL/SQL, Visual Basic, Modula-2, and AppleScript.

    Disallowing the statement following a "then" to be an "if" itself (it may however be a pair of statement brackets containing only an if-then-clause). This approach is followed by ALGOL 60.[8]

    Requiring braces (parenthesize) when an "else" follows an "if".[9]

    Requiring every "if" to be paired with an "else". To avoid a similar problem concerning semantics rather than syntax, Racket deviates from Scheme by considering an if without a fallback clause to be an error, effectively distinguishing conditional (i.e if) from conditional (i.e when and unless, which do not have fallback clauses).

    Using different keywords for the one-alternative and two-alternative "if" statements. S-algol uses if e do s for the one-alternative case and if e1 then e2 else e3 for the general case.[10]

    Requiring braces unconditionally, like Swift. This is effectively true in Python as its indentation rules delimit every block, not just those in "if" statements.

    Examples[edit]

    Concrete examples follow.

    C[edit]

    In C, the grammar reads, in part:

    statement = ...

    | selection-statement

    selection-statement = ...

    | IF ( expression ) statement

    | IF ( expression ) statement ELSE statement

    Thus, without further rules, the statement

    if (a) if (b) s; else s2;

    could ambiguously be parsed as if it were either:

    if (a) { if (b) s; else s2; } or: if (a) { if (b) s; } else s2;

    In practice in C the first tree is chosen, by associating the else with the nearest if.

    Avoiding the conflict in LR parsers[edit]

    The above example could be rewritten in the following way to remove the ambiguity :

    statement: open_statement

    | closed_statement ;

    open_statement: IF '(' expression ')' statement

    | IF '(' expression ')' closed_statement ELSE open_statement

    ;

    closed_statement: non_if_statement

    | IF '(' expression ')' closed_statement ELSE closed_statement

    स्रोत : en.wikipedia.org

    Dangling

    A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

    Dangling-else Ambiguity

    Difficulty Level : Medium

    Last Updated : 27 Dec, 2021

    Read Discuss

    Compilers and interpreters use grammar to build the data structure in order to process the programs. So ideally one program should have one derivation tree. A parse tree or a derivation tree is a graphical representation that shows how strings of the grammar are derived using production rules. But there exist some strings which are ambiguous.

    A grammar is said to be ambiguous if there exists more than one leftmost derivation or more than one rightmost derivation or more than one parse tree for an input string. An ambiguous grammar or string can have multiple meanings. Ambiguity is often treated as a grammar bug, in programming languages, this is mostly unintended.

    Dangling-else ambiguity

    The dangling else problem in syntactic ambiguity. It occurs when we use . When there are multiple statements, the part doesn’t get a clear view with which “ ” it should combine.

    if (condition) { } if (condition 1) { } if (condition 2) { } else { }

    In the above example, there are multiple “ with multiple conditions and here we want to pair the outermost if with the else part. But the else part doesn’t get a clear view with which “ condition it should pair. This leads to inappropriate results in programming.

    The Problem of Dangling-else:

    Dangling else can lead to serious problems. It can lead to wrong interpretations by the compiler and ultimately lead to wrong results.

    Initialize k=0 and o=0

    if(ch>=3) if(ch<=10) k++; else o++;

    In this case, we don’t know when the variable “” will get incremented. Either the first “” condition might not get satisfied or the second “” condition might not get satisfied. Even the first “” condition gets satisfied, the second “ condition might fail which can lead to the execution of the “ part. Thus it leads to wrong results.

    To solve the issue the programming languages like C, C++, Java combine the “ part with the innermost “ statement. But sometimes we want the outermost “ statement to get combined with the “ part.

    Resolving Dangling-else Problem

    The way is to design non-ambiguous programming languages.

    , we can resolve the dangling-else problems in programming languages by using braces and indentation.

    if (condition) { if (condition 1) { if (condition 2) {} } } else { }

    In the above example, we are using braces and indentation so as to avoid confusion.

    , we can also useformat so as to specifically indicate which “ belongs to which “.

    if(condition) { }

    else if(condition-1) {

    }

    else if(condition-2){

    } else{ }

    स्रोत : www.geeksforgeeks.org

    Dangling Else or If

    Presents a way to overcome the if-else ambiguity by syntax alone, yielding a conflict-free syntax without the need for disambiguation rules

    HomeTrial Copy

    Intro. to Parsing Users Say... Special Features Notation Summary New 2.01 Features File Trace Grammar Trace Glossary Examples

    Expression evaluator (freeware)

    XIDEK interpreter kit (freeware)

    Lex/Yacc Comparison If-else ambiguity

    Contact Parsifal

    Resolving the General Dangling Else/If-Else Ambiguity

    Description of the ambiguity

    An unambiguous syntax for statements

    Developing the conflict-free syntax

    A troublesome feature of grammars for many programming languages, including C and C++, is a recurring problem known variously as dangling else, if-else ambiguity, or if-else conflict. We show a way to overcome this problem by syntax alone, yielding a conflict-free syntax without the need for "disambiguation rules". In addition, we provide an overview of the algebraic process used to develop the conflict-free syntax. In the following discussion, we use the notation of the AnaGram LALR parser generator to describe the syntax.

    Description of the Ambiguity

    Commonly, the syntax for if-else statements is written thus:

    if statement

    -> if clause, statement

    -> if clause, statement, "else", statement

    statement -> simple statement -> if statement -> loop statement

    where subsumes expression statements, block statements, and statements, and even the statement; in short, any statement which is neither left nor right recursive. subsumes statements and statements, i.e., right recursive statements of the form:

    loop statement

    -> loop header, statement

    This syntax is ambiguous, as can be illustrated by the following example:

    if (conditionA) if (conditionB) statementA; else statementB;

    Using the syntax given above, this statement can be interpreted either as:

    if (conditionA) {

    if (conditionB) statementA; else statementB;

    } or as: if (conditionA) {

    if (conditionB) statementA;

    } else statementB;

    Both interpretations are consistent with the syntax definition given above, but they have very different outcomes. Conventionally, parsers are rigged using some sort of trick to select the first interpretation. This trick is often called a "disambiguation rule". In AnaGram, you could use the attribute statement:

    [ sticky {statement} ]

    However, such tricks are not necessary, since the statement syntax can be made conflict-free as described below. A simpler conflict-free syntax for statements is provided as an example in section 4.3 of "Compilers: Principles, Techniques and Tools" by Aho, Sethi and Ullman, but their syntax is incomplete in that it does not, in fact, provide for right-recursive statements such as and .

    An Unambiguous Syntax for Statements

    The problem with the conventional syntax for statements is that it represents a simple taxonomy of statements rather than an appropriate description of syntax. Although it is true that there are statements, loop statements and simple statements, this is not the most useful way to classify statements from a syntactic point of view.

    The following syntax requires a following to be paired with the most recent unpaired , thus disallowing the if-else ambiguity.

    We define an open statement as one which has at least one that is not paired with a following within the statement. A closed statement either does not contain an at all, or if it does, the is paired with a following . We can then write the statement syntax as follows:

    statement -> open statement -> closed statement open statement

    -> if clause, statement

    -> if clause, closed statement, "else", open statement

    -> loop header, open statement

    closed statement -> simple statement

    -> if clause, closed statement, "else", closed statement

    -> loop header, closed statement

    In this syntax, we allow only closed statements between an if and its matching else. In other words, between an if and an else an if is allowed if it is paired with a matching else clause. The net effect is that each else is associated with the nearest preceding if.

    In the next section we will show how this syntax can be developed by suitably rewriting our original syntax.

    Developing the Conflict-Free Syntax

    To see how to resolve the ambiguity and develop the above syntax, let us begin with the customary ambiguous syntax described in the first section and try rewriting as

    statement -> open statement -> closed statement

    where, as described above, is any statement that has a "dangling" , that is, an which could be paired with a following . A closed statement is one which does not have a dangling if.

    Substituting the above into the second rule for yields:

    if statement

    -> if clause, statement

    -> if clause, closed statement, "else", statement

    -> if clause, open statement, "else", statement

    Clearly, the last of these three rules for is always ambiguous, since , by definition, contains one or more s not paired with s, and thus it is not clear which should be associated with the .

    Therefore, let us eliminate the last rule, leaving our rules for as follows:

    if statement

    -> if clause, statement

    -> if clause, closed statement, "else", statement

    स्रोत : www.parsifalsoft.com

    Do you want to see answer or more ?
    Mohammed 1 month ago
    4

    Guys, does anyone know the answer?

    Click For Answer