[OS X TeX] finding missing braces

Roussanka Loukanova rloukano at stp.lingfil.uu.se
Tue Mar 6 06:40:17 EST 2007

Hi,

On Mon, 5 Mar 2007, Herbert Schulz wrote:

>
> On Mar 4, 2007, at 5:05 PM, Roussanka Loukanova wrote:
>
>>> Howdy,
>>>
>>> I believe the problem is intractable. How can a program know what you
>>> intended? E.g., suppose you leave off a closing brace in a sentence,
>>>
>>> \emph{emphasized text ...
>>>
>>> how will a program know whether the closing brace goes after the word
>>> emphasized' or the word text', or ...?
>>
>> Right: In cases like the above, \emph{emphasized text ..., the error notice
>> in the log (e.g., the console in TeXShop) may say that the scope of the
>> unmatched "{" goes to the last line of the entire tex file.
>>
>
> Howdy,
>
> Just as a note: since \emph{...} is meant for short snippets it will stop and
> complain if you come the end of a paragraph and it hasn't reached a closing
> brace. I.e., it is a short'' command.

A straight note since I did miss to pay attention to this specific feature
of \emph{...}. I used the word "may" for far more general reasons.
But, the \emph{... is a good example as a reason for a (compiler-like)
program to leave the possible location of the missing closing brace
in similar cases unspecified, since this is only up to the user's
intentions.

Other than this specific feature of \emph{...}, the error notice may
point to the end of a scope which encompasses an unmatched brace (or
a similar delimiter), and that may be the end of a scope which is not the
narrowest enclosing one.

Detection of mismatched delimiters and error notice, in general, depend
on what algorithm for well-balanced delimiters ({, }, (, ), $, etc.) has been implemented (in this case, in TeX, and also, in TeXShop or Emacs, etc. editors, which we use not only to type, but also to get detected balanced-unbalanced delimiters, and, ideally, to see their scope, for ex. by: the text between matched delimiters is highlighted; the cursor jumps to the matching opening brace/parenthesis/$.

There exists no algorithm that would simply count the number of
occurrences of opening and closing delimiters to check for
well-matching, just by looking if the two numbers are equal.

A popular simple algorithm uses an auxiliary stack to check for
matched and unmatched delimiters as follows (loosely described):

Read the characters of the input string (representing the text) from left
to right

Case 1: the input is not (yet) empty

- for each input "("
= save a copy of "(" on the top of the stack

- for each input ")"
= delete the "(" from the top of the stack, if there is such "(", (this
is the matching step), otherwise
= i.e., the stack is empty: stop and output "unbalanced )"

Case 2: the input is empty

- the stack is empty: OK, otherwise
- i.e., the stack has a "(" on its top: stop and output "unbalanced ("

Roussanka