KUWAIT, AUG. 9 -- The Iranian-sparked violence that swept the holy city of Mecca during the recent annual pilgrimage has left the moderate Arab states along the southwestern rim of the Persian Gulf shocked and apprehensive.

It has shaken these nations' hopes that they could remain neutral and avoid being destabilized by the Iran-Iraq war that has raged at the northern end of the gulf for nearly seven years. But, even more than the continuing international military buildup in the gulf, the violence in Mecca has reminded them that the perils they face will not necessarily end with a cease-fire in the war.

For the real meaning of the riots that left at least 400 people dead in Islam's holiest of shrines is that the Iran-Iraq conflict is not just an isolated political and territorial war between two neighboring gulf states with an age-old history of antagonism.

It is, instead, a much broader historic struggle being waged by Iran's revolutionary clerics, or mullahs, to reverse 12 centuries of Moslem history and impose their splinter Shiite sect's dominance over the mainstream Sunni group to which the vast majority of Moslems around the world adhere.

That Iran's spiritual leader Ayatollah Ruhollah Khomeini chose the sacred hajj, or annual pilgrimage to Mecca, as the time to order its 250,000 pilgrims to stage a violent political demonstration in that religious place is seen here by Arab and western analysts as proof that Iran's aims are more than settling a score with Iraqi President Saddam Hussein.

"What everyone has tended to forget because of their focus on the Iran-Iraq war is that it is but a step, a first step, in Ayatollah Khomeini's grand dream of uniting all of his fellow Moslems into one single revolutionary Moslem state that would stretch from Morocco to Indonesia," said a Palestinian political scientist at Kuwait University who asked not to be named.

"Iran's brand of revolutionary Islam is just supposed to be the model for the larger united Islamic state Khomeini wants to create," the professor said. "Khomeini is a visionary, a religious zealot with a mission, not just an ordinary politican. He is not interested in Iran per se. He wants to be the prophet who revitalized, united and spread Islam."

Proof of this mission, other analysts here pointed out, is to be seen in the message that Khomeini, 87, sent the Mecca pilgrims two days before the July 31 riot.

Khomeini ordered the pilgrims to stage an anti-American demonstration because "if Moslems cannot denounce the enemies of God in their own home, where can they?" He then went on to talk of his Islamic revolution in almost global terms.

"With confidence, I tell you . . . that Islam will eliminate all the great obstacles, internally as well as externally beyond its frontiers, and will conquer the principal bastions in the world," Khomeini assured the pilgrims.

{On the CBS television program "Face the Nation" Sunday, U.S. Secretary of Defense Caspar W. Weinberger said that the Iranians "were responsible for the carnage at Mecca" and that many of the Iranian pilgrims who came to Saudi Arabia were stopped at the border and "had explosives . . . pistols . . . ammunition. They have every means of destroying people and creating an incident . . . and they did it."}

After the riots, Khomeini not only denounced the Saudi ruling family in insulting terms but proposed that they be stripped of the responsibility of being the guardians of Mecca and Medina, Islam's two holiest shrines.

The Iranian threat to the Saudis is not taken lightly here. With Iraq weakened and bogged down in war, Saudi Arabia is not only the guardian of the holy places but also the guardian of all the tiny, moderate and militarily weak sheikdoms that dot the southwestern coast of the Persian Gulf.

The challenge to Saudi Arabia, by far the most powerful of the Arabian nations economically and militarily, is also a direct challenge to Kuwait, Bahrain, Qatar and the the six states of the United Arab Emirates.

Until the incident at Mecca these nations had sought to maintain an official neutrality, although Kuwait's was always questionable because of the ports and cash subsidies it provided neighboring Iraq.

Even Saudi Arabia, which, along with Kuwait, loaned the Iraqis billions of dollars over the years, recently had sought dialogue and cooperation with Iran.

In 1985, the Saudi and Iranian foreign ministers visited each other's capitals and, as a result, Saudi Arabia began providing Iran with refined oil products Iran could no longer produce for itself because of Iraqi bombing of its refineries.

Last year the relationship led the two nations to cooperate within the OPEC oil cartel to impose production limits that succeeded in raising the price of oil, a boon to dollar-depleted Iran and a reversal of previous Saudi production policies.

That those gestures, of no small importance to Iran, were not enough to keep the rulers in Tehran from trying to destabilize Saudi Arabia has shocked the smaller Sunni-dominated gulf nations, which now feel more vulnerable than ever.

"Until Mecca," said one diplomat, "there was a feeling here that there were limits beyond which Iran would not step. Now all that is changed. No one knows how far Iran is prepared to go here."

What is so difficult for the nonrevolutionary governments of the area is the realization that their efforts to accommodate Iran have not worked and yet they are too weak to defy their threatening neighbor openly.

"The nations here have followed a policy of trying to appease Iran without openly turning against their fellow Arabs in Baghdad," noted one western diplomat here. "In one action that policy has been proven unrealistic and inadequate and no one is sure just where to go from here."

No one expects Iran, with its military tied by the ground war with Iraq, to contemplate any open military actions against Saudi Arabia or one of the sheikdoms. But there is fear that its leaders will try to make good on their promises of revenge by staging some new terrorist acts against Saudi Arabia or Kuwait.