I am making a parser with Bison and Flex and I want to create a "goto label" statement, but I want to check if the label exists in the same block of code (between brackets { }, loop, etc).
Is there a function that checks such things?
I am making a parser with Bison and Flex and I want to create a "goto label" statement, but I want to check if the label exists in the same block of code (between brackets { }, loop, etc).
Is there a function that checks such things?
Your question implies that you are missing some background context in building language translators/compilers, so perhaps a small tutorial will assist you in solving your problem. I hope you don't mind.
The processing of computer languages is conventionally divided up into a sequence of steps (sometimes called phases or passes). Each step handles a component of the whole task, and each step makes the step that follows easier to implement.
The steps would be:
Concurrent with all those steps a global data structure is usually maintained, called the Symbol Table. The purpose of the symbol table is to keep track of the names used in the program being compiled and the attributes of all those symbols. A label in a goto is such a symbol that would be recorded in the symbol table.
The first step, lexical analysis, is where the symbols are discovered as the different lexemes are converted into tokens of the language. Not all lexemes found by the lexer will cause entries to be made into the symbol table. Things such as keywords in the language, comments and such like do not usually have symbol table entries. Identifiers, such as the label in a goto, would cause entries to be made in the symbol table. The first time such an identifier is encountered a new table entry would be made; a note might be made of the line where it was encountered. Subsequent occurrences of the identifier would not cause new table entries to be made, however information may be updated.
At the lexical stage no errors regarding label ordering could be generated as label references could be either forward or back†:
Neither the first or second occurrence of a label identifier would indicate the label is undefined.
In syntax analysis we would have a similar problem. The syntax rules would determine when a valid label definition is encountered and also when a valid goto statements is found. However there is still not enough information to decide if the label used is a valid one. This is particularly true when scoping of the label is concerned. Scoping information is something that would have to be resolved by consulting the symbol table after parsing has completed. All the parser can do is record the valid parse, usually in the form of a parse tree.
It is the semantic analysis stage, which operates on the parse tree that has all the information available to determine which goto labels are valid and which are not. It does this by adding further information to the symbol table, such as scoping, recording which labels have been declared and where. It is then possible to see which gotos refer to an undefined label and issue an appropriate message.
The flex/bison (or lex/yacc) tool sets are commonly used form of compiler-compiler. Tools used to build the specifications of compilers into the compiler. There are many others to choose from.
The flex/bison tools do not include a package for a symbol table (or a parse tree) and it is necessary for programmers that use these tools to implement their own. Once you have implemented your own symbol table data structure and provided functions for determining attributes of the symbols stored within it, it then becomes possible to enquire if a goto label is undefined.
So, in summary, the answer is no; there is no built in way of doing what you wanted as it is outside the scope of the tools you are using‡.
† This example is in Algol 60.
‡ Which is what @rici said.