search
Log In
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
Welcome to GATE CSE Doubts, where you can ask questions and receive answers from other members of the community.
0 votes
20 views
Errors violating the condition ‘any variable must be declared before its use’ are detected during semantic analysis.
Why cannot it come under syntax analysis?

Does declaration not fall under Syntax?
in Compiler Design 7 points 20 views

1 Answer

0 votes
 
Best answer

@GreyMatter

 

Errors violating the condition ‘any variable must be declared before its use’ are detected during semantic analysis.

Why cannot it come under syntax analysis? .…

 

When an input string (source code or a program in some language) is given to a compiler, the compiler processes it in several phases, starting from lexical analysis (scans the input and divides it into tokens) to target code generation.

Syntax Analysis or Parsing is the second phase, i.e. after lexical analysis.

It checks the syntactical structure of the given input, i.e. whether the given input is in the correct syntax (of the language in which the input has been written) or not. It does so by building a data structure, called a Parse tree or Syntax tree….

 

 

The parse tree is constructed by using the pre-defined Grammar of the language and the input string.

If the given input string can be produced with the help of the syntax tree (in the derivation process), the input string is found to be in the correct syntax. if not, error is reported by syntax analyzer.

The Grammar for a Language consists of Production rules....

 

https://www.guru99.com/syntax-analysis-parsing-types.html

Variable declare before its use must be done by Semantic analyses....

 

Syntax analyser uses CFL which cannot check for this, we need power of Context sensitive language which is available in semantic analysis phase.

So this error is detected only during semantic analysis phase.…

Syntax analysis checks only syntax, but variable declare before its use must be done by Semantic analyses....

633 points
selected by
0

@GreyMatter

 

Does declaration not fall under Syntax?

This section describes a grammar (and forward-compatible parsing rules) common to any level of CSS . Future updates of CSS will adhere to this core syntax, although they may add additional syntactic constraints...

In this specification, the expressions "immediately before" or "immediately after" mean with no intervening white space or comments...

 

https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node104.html  

 

https://www.w3.org/TR/CSS2/syndata.html  

 

https://en.wikipedia.org/wiki/Declaration_(computer_programming) 

0
You say that error for no declaration of a variable prior to its use is given by Semantic analyser.

But declaration comes with type of the variable (for eg.- int x) and this declaration where there’s type followed by variable name, is this not syntax?
0

@GreyMatter

But declaration comes with type of the variable (for eg.- int x) and this declaration where there’s type followed by variable name, is this not syntax ...

 

A variable in simple terms is a storage place which has some memory allocated to it. Basically, a variable used to store some form of data.

Different types of variables require different amounts of memory, and have some specific set of operations which can be applied on them.…

Variable Declaration: A typical variable declaration is of the form: type variable_name; or for multiple variables: type variable1_name, variable2_name, variable3_name;

A variable name can consist of alphabets (both upper and lower case), numbers and the underscore ‘_’ character. However, the name must not start with a number....

 

https://gateoverflow.in/357537/gate-cse-2021-set-2-question-3  

 

https://gateoverflow.in/118293/gate-cse-2017-set-1-question-13  

 

https://gateoverflow.in/118293/gate-cse-2017-set-1-question-13?show=120599#a120599 

 

https://www.cs.utah.edu/~germain/PPS/Topics/variables.html 

...